From c75ab6042f40f9101e88d41c1d41b5e41c3f12ef Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Tue, 26 Dec 2023 15:46:04 -0600 Subject: Add increment type --- lib/ast_types.ml | 5 +++++ lib/fixture.ml | 12 +++++++++--- lib/lexer.mll | 1 + lib/parser.mly | 5 +++++ 4 files changed, 20 insertions(+), 3 deletions(-) (limited to 'lib') 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 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) } -- cgit v1.2.3