(* [to_float] reads a value as numeric affinity does: a number is itself, a text is the number it spells in full, and anything else is no number. *) let check_float name want v = Alcotest.(check (option (float 0.))) name want (Sqlite.Value.to_float v) let test_to_float () = check_float "int" (Some 42.) (Sqlite.Int 42L); check_float "negative int" (Some (-7.)) (Sqlite.Int (-7L)); check_float "float" (Some 2.5) (Sqlite.Float 2.5); check_float "integer text" (Some 12.) (Sqlite.Text "12"); check_float "real text" (Some 3.5) (Sqlite.Text "3.5"); check_float "exponent text" (Some 1000.) (Sqlite.Text "1e3"); check_float "padded text" (Some 12.) (Sqlite.Text " 12 "); check_float "trailing garbage" None (Sqlite.Text "12abc"); check_float "word" None (Sqlite.Text "abc"); check_float "empty text" None (Sqlite.Text ""); check_float "hex text" None (Sqlite.Text "0x10"); check_float "null" None Sqlite.Null; check_float "blob" None (Sqlite.Blob "12") let test_text () = let check name want v = Alcotest.(check (option string)) name want (Sqlite.Value.text v) in check "text" (Some "abc") (Sqlite.Text "abc"); check "empty text" (Some "") (Sqlite.Text ""); check "numeric text" (Some "12") (Sqlite.Text "12"); check "int" None (Sqlite.Int 12L); check "float" None (Sqlite.Float 1.5); check "null" None Sqlite.Null; check "blob" None (Sqlite.Blob "abc") (* [to_shell] is the cell sqlite3 3.54.1 prints in list mode: `sqlite3 :memory: "SELECT x'0001ff41', 'a'||char(27)||'b', 'c'||char(13)||char(10)||'d'"` prints ^@^A\xffA|a^[b|c\r\nd. A text cell stops at its first NUL, a blob cell does not. *) let test_to_shell () = let check name want v = Alcotest.(check string) name want (Sqlite.Value.to_shell v) in check "blob" "^@^A\xffA" (Sqlite.Blob "\x00\x01\xffA"); check "escape" "a^[b" (Sqlite.Text "a\x1bb"); check "crlf kept" "c\r\nd" (Sqlite.Text "c\r\nd"); check "lone cr" "c^Md" (Sqlite.Text "c\rd"); check "tab kept" "a\tb" (Sqlite.Text "a\tb"); check "text stops at nul" "a" (Sqlite.Text "a\x00^b"); check "null" "" Sqlite.Null; check "real" "1.0" (Sqlite.Float 1.) let suite = ( "value", [ Alcotest.test_case "to_float" `Quick test_to_float; Alcotest.test_case "text" `Quick test_text; Alcotest.test_case "to_shell" `Quick test_to_shell; ] )