aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Coquand <marc@mccd.space>2023-12-26 15:46:04 -0600
committerMarc Coquand <marc@mccd.space>2023-12-26 15:46:04 -0600
commitc75ab6042f40f9101e88d41c1d41b5e41c3f12ef (patch)
treed21b6771833e07b6f4b6cb58c4fbce109485d7c8
parent508c7c9b34a892d74d087f1ef5d54d16fa000551 (diff)
downloadfixgen-c75ab6042f40f9101e88d41c1d41b5e41c3f12ef.tar.gz
fixgen-c75ab6042f40f9101e88d41c1d41b5e41c3f12ef.tar.bz2
fixgen-c75ab6042f40f9101e88d41c1d41b5e41c3f12ef.zip
Add increment type
-rw-r--r--README.md1
-rw-r--r--lib/ast_types.ml5
-rw-r--r--lib/fixture.ml12
-rw-r--r--lib/lexer.mll1
-rw-r--r--lib/parser.mly5
-rw-r--r--test/test_parser.ml2
6 files changed, 22 insertions, 4 deletions
diff --git a/README.md b/README.md
index c4026b0..fc407ed 100644
--- a/README.md
+++ b/README.md
@@ -49,6 +49,7 @@ Fixgen comes with the following built-in types:
- int(min,max)
- constants, defined using quotes I.E. `$ fixgen 'user (1): id uuiv4, name "Bob"'`
- ("multiple","choices")
+- increment, `$ fixgen 'user (4): id increment' -> id\n1\n2\n3\n4
## Development - Prerequisites
diff --git a/lib/ast_types.ml b/lib/ast_types.ml
index 2a786f7..6a02040 100644
--- a/lib/ast_types.ml
+++ b/lib/ast_types.ml
@@ -5,6 +5,7 @@ type ast_row =
| Uuidv4 of string * ast_row
| Name of string * ast_row
| Int of string * int * int * ast_row
+ | Increment of string * ast_row
| Const of string * string * ast_row
| List of string * string list * ast_row
| Foreign of string * string * string * ast_row
@@ -27,6 +28,9 @@ let rec print_row = function
| List (s, l, r) ->
printf "List(%s, %s)," s (String.concat ", " l);
print_row r
+ | Increment (s, r) ->
+ printf "Increment(%s)," s;
+ print_row r
| Const (s, v, r) ->
printf "Const(%s, %s)," s v;
print_row r
@@ -75,6 +79,7 @@ let rec ast_row_to_fixtures = function
| Int (s, min, max, r) -> PColumn (Fixture.Int (s, min, max)) :: ast_row_to_fixtures r
| Const (s, v, r) -> PColumn (Fixture.Const (s, v)) :: ast_row_to_fixtures r
| List (s, l, r) -> PColumn (Fixture.List (s, l)) :: ast_row_to_fixtures r
+ | Increment (s, r) -> PColumn (Fixture.Increment s) :: ast_row_to_fixtures r
| End -> []
let extend_table_with_row table row =
diff --git a/lib/fixture.ml b/lib/fixture.ml
index 6080fd9..21d77cf 100644
--- a/lib/fixture.ml
+++ b/lib/fixture.ml
@@ -60,15 +60,17 @@ type t =
| Uuidv4 of string
| Foreign of string * compiled list
| Int of string * int * int
+ | Increment of string
| Const of string * string
| List of string * string list
(* (Name, foreign ids to pick from) *)
-let generate_fixture fixture =
+let generate_fixture index fixture =
match fixture with
| Name _ -> String (random_name ())
| Uuidv4 _ -> String (Uuidm.v `V4 |> Uuidm.to_string)
| Foreign (_, reference) -> random_value_in_list reference
+ | Increment _ -> Int (index + 1)
| Const (_, value) -> String value
| Int (_, min, max) -> Int (Random.int (max - min) + min)
| List (_, values) -> String (random_value_in_list values)
@@ -95,7 +97,7 @@ let rec replicate element n =
let%test "generate_uuid" =
- generate_fixture (Uuidv4 "some_id") != generate_fixture (Uuidv4 "some_id")
+ generate_fixture 1 (Uuidv4 "some_id") != generate_fixture 1 (Uuidv4 "some_id")
let id_of_fixture fixture =
@@ -103,6 +105,7 @@ let id_of_fixture fixture =
match fixture with
| Name id -> id
| Uuidv4 id -> id
+ | Increment id -> id
| Foreign (id, _) -> id
| Const (id, _) -> id
| Int (id, _, _) -> id
@@ -113,7 +116,10 @@ let id_of_fixture fixture =
let compile fixtures ~amount =
let identifiers = List.map id_of_fixture fixtures in
- let values = replicate fixtures amount |> List.map (List.map generate_fixture) in
+ let values =
+ replicate fixtures amount
+ |> List.mapi (fun index -> List.map (generate_fixture index))
+ in
identifiers :: values
diff --git a/lib/lexer.mll b/lib/lexer.mll
index b4e26d8..e733666 100644
--- a/lib/lexer.mll
+++ b/lib/lexer.mll
@@ -20,6 +20,7 @@ rule read =
parse
| "uuidv4" { UUIDV4 }
| "name" { NAME }
+ | "increment" { INCREMENT }
| "int" { INTSYMBOL }
| id { IDENTIFIER (Lexing.lexeme lexbuf) }
| "(" { LBRACE }
diff --git a/lib/parser.mly b/lib/parser.mly
index 19fa44e..6172963 100644
--- a/lib/parser.mly
+++ b/lib/parser.mly
@@ -2,6 +2,7 @@
open Ast_types
%}
%token COMMA
+%token INCREMENT
%token <string> IDENTIFIER
%token UUIDV4
%token NEWLINE
@@ -26,6 +27,7 @@ id_or_other:
| IDENTIFIER { $1 }
| NAME { "name" }
| UUIDV4 { "uuidv4" }
+ | INCREMENT { "increment" }
expr:
@@ -47,6 +49,9 @@ row:
| row_title = id_or_other; NAME; COMMA; r = row { Name (row_title, r) }
| row_title = id_or_other; NAME { Name (row_title, End) }
+ | row_title = id_or_other; INCREMENT; COMMA; r = row { Increment (row_title, r) }
+ | row_title = id_or_other; INCREMENT { Increment (row_title, End) }
+
| row_title = id_or_other; const = STRING; COMMA; r = row { Const (row_title, const, r) }
| row_title = id_or_other; const = STRING { Const (row_title, const, End) }
diff --git a/test/test_parser.ml b/test/test_parser.ml
index e18f055..db18540 100644
--- a/test/test_parser.ml
+++ b/test/test_parser.ml
@@ -27,7 +27,7 @@ let rows_suite =
, `Quick
, test_parse "purchases (5): name int(1,20)" )
; "Supports underscore tables", `Quick, test_parse "purchases_new (5): name int(1,20)"
- ; "Supports underscore tables", `Quick, test_parse "purchases_new (5): name int(1,20)"
+ ; "Supports incremental index", `Quick, test_parse "purchases_new (5): id increment"
]