aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Coquand <marc@mccd.space>2023-12-26 10:46:11 -0600
committerMarc Coquand <marc@mccd.space>2023-12-26 10:46:11 -0600
commit2a664a47c73e232b6ff0d261833e2561d4abc4d0 (patch)
tree8ad1f0c88f6151bff69f63d61e1e447d2ae5b034
parentd96e1839eb800bf26bcc38272072d98af69f5d83 (diff)
downloadfixgen-2a664a47c73e232b6ff0d261833e2561d4abc4d0.tar.gz
fixgen-2a664a47c73e232b6ff0d261833e2561d4abc4d0.tar.bz2
fixgen-2a664a47c73e232b6ff0d261833e2561d4abc4d0.zip
Support list
-rw-r--r--lib/ast_types.ml7
-rw-r--r--lib/fixture.ml6
-rw-r--r--lib/parser.mly7
-rw-r--r--test/test_parser.ml3
4 files changed, 21 insertions, 2 deletions
diff --git a/lib/ast_types.ml b/lib/ast_types.ml
index c278d59..c192c60 100644
--- a/lib/ast_types.ml
+++ b/lib/ast_types.ml
@@ -6,6 +6,7 @@ type ast_row =
| Name of string * ast_row
| Int of string * int * int * ast_row
| Const of string * string * ast_row
+ | List of string * string list * ast_row
| Foreign of string * string * string * ast_row
(* parent, row, child_name, row *)
| End
@@ -15,11 +16,16 @@ type ast_table =
| Table of string * int * ast_row * ast_table
| End
[@@deriving show, eq]
+(** In hindsight, this could've been represented as a list of tables instead using separated list command
+ TODO: Rewrite this to use a list of tables instead of a tree *)
let rec print_row = function
| Uuidv4 (s, r) ->
printf "UUIDv4(%s)," s;
print_row r
+ | List (s, l, r) ->
+ printf "List(%s, %s)," s (String.concat ", " l);
+ print_row r
| Const (s, v, r) ->
printf "Const(%s, %s)," s v;
print_row r
@@ -65,6 +71,7 @@ let rec ast_row_to_fixtures = function
| Name (s, r) -> PRow (Fixture.Name s) :: ast_row_to_fixtures r
| Int (s, min, max, r) -> PRow (Fixture.Int (s, min, max)) :: ast_row_to_fixtures r
| Const (s, v, r) -> PRow (Fixture.Const (s, v)) :: ast_row_to_fixtures r
+ | List (s, l, r) -> PRow (Fixture.List (s, l)) :: ast_row_to_fixtures r
| End -> []
diff --git a/lib/fixture.ml b/lib/fixture.ml
index 2b17a02..d5c77fa 100644
--- a/lib/fixture.ml
+++ b/lib/fixture.ml
@@ -25,9 +25,10 @@ let%test "random_name" = List.mem (random_name ()) names
type t =
| Name of string
| Uuidv4 of string
- | Foreign of (string * string list)
+ | Foreign of string * string list
| Int of string * int * int
| Const of string * string
+ | List of string * string list
(* (Name, foreign ids to pick from) *)
let add_name name fixtures = Name name :: fixtures
@@ -48,6 +49,7 @@ let generate_fixture fixture =
| Foreign (_, reference) -> random_value_in_list reference
| Const (_, value) -> value
| Int (_, min, max) -> Random.int (max - min) + min |> string_of_int
+ | List (_, values) -> random_value_in_list values
let%test "generate_uuid" =
@@ -61,10 +63,10 @@ let id_of_fixture fixture =
| Foreign (id, _) -> id
| Const (id, _) -> id
| Int (id, _, _) -> id
+ | List (id, _) -> id
(* TODO: Support email *)
-(* TODO: Support list *)
let rec replicate element n =
match n with
diff --git a/lib/parser.mly b/lib/parser.mly
index 6e21480..8a666c6 100644
--- a/lib/parser.mly
+++ b/lib/parser.mly
@@ -35,6 +35,9 @@ row:
| row_title = IDENTIFIER; UUIDV4; COMMA; r = row { Uuidv4 (row_title, r) }
| row_title = IDENTIFIER; UUIDV4 { Uuidv4 (row_title, End) }
+ | row_title = IDENTIFIER; LBRACE; union = union_fields; RBRACE; COMMA; r = row { List (row_title, union, r) }
+ | row_title = IDENTIFIER; LBRACE; union = union_fields; RBRACE { List (row_title, union, End) }
+
| row_title = IDENTIFIER; NAME; COMMA; r = row { Name (row_title, r) }
| row_title = IDENTIFIER; NAME { Name (row_title, End) }
@@ -44,3 +47,7 @@ row:
| row_title = IDENTIFIER; INTSYMBOL;LBRACE;min = INT;COMMA;max = INT;RBRACE;COMMA; r = row { Int (row_title,min,max,r) }
| row_title = IDENTIFIER; INTSYMBOL;LBRACE;min = INT;COMMA;max = INT;RBRACE; { Int (row_title,min,max,End) }
;
+
+union_fields:
+ vl = separated_list(COMMA, STRING) { vl }
+ ;
diff --git a/test/test_parser.ml b/test/test_parser.ml
index abb0a40..80e9d12 100644
--- a/test/test_parser.ml
+++ b/test/test_parser.ml
@@ -20,6 +20,9 @@ let rows_suite =
; "Supports names", `Quick, test_parse "purchases (1): name _name"
; "Supports const", `Quick, test_parse "purchases (1): name \"hello\""
; "Supports ints with min/max", `Quick, test_parse "purchases (1): name _int(1,20)"
+ ; ( "Supports a list of potential values"
+ , `Quick
+ , test_parse "purchases (1): name (\"a\",\"b\",\"c\")" )
; ( "Supports setting amount of fixture to generate"
, `Quick
, test_parse "purchases (5): name _int(1,20)" )