diff --git a/cmd/parse_builtins/main.go b/cmd/parse_builtins/main.go index 73eb7812..8227300a 100644 --- a/cmd/parse_builtins/main.go +++ b/cmd/parse_builtins/main.go @@ -45,6 +45,14 @@ func getCommentsAboveKey(fset *token.FileSet, comments []*ast.CommentGroup, keyP // Helper function to get comments above the map key func parseCommentsAboveKey(input string, info *builtinInfo) builtinInfo { + const ( + inDoc = iota + inTests + inExamples + inArgs + ) + position := inDoc + // info := builtinInfo{} // Step 1: Split input into lines and trim whitespace @@ -53,7 +61,6 @@ func parseCommentsAboveKey(input string, info *builtinInfo) builtinInfo { // Step 2: Separate header and tests var headerLines []string // var testLines []string - isTestSection := false // fmt.Println("!!!!!!!!!!!!!!!**************") @@ -61,17 +68,27 @@ func parseCommentsAboveKey(input string, info *builtinInfo) builtinInfo { line = strings.TrimSpace(line) // Remove leading and trailing whitespace // fmt.Println("LLLL:" + line) // fmt.Println(line) - if line == "Tests:" { - // fmt.Println("***** TEST ****") - isTestSection = true + switch line { + case "Tests:": + position = inTests + continue + case "Examples:": + position = inExamples + continue + case "Args:": + position = inArgs continue } - if isTestSection { - // fmt.Println(line) + switch position { + case inTests: info.tests = append(info.tests, line) - } else { + case inDoc: headerLines = append(headerLines, line) + case inExamples: + info.examples = append(info.examples, line) // TODO --- examples can be multiline, there is a name also + case inArgs: + info.args = append(info.args, line) } } // Step 3: Combine the header lines into a single string @@ -85,7 +102,14 @@ func outputInfo(infos []builtinInfo) { if len(info.tests) > 0 { fmt.Printf("\tgroup %s \n", info.name) // name fmt.Printf("\t\"%s\"\n", info.docstring) // docstring - fmt.Print("\t{ }\n\t{\n") // args + + fmt.Print("\t{\n") // args + for _, t := range info.args { + fmt.Println("\t\targ \"" + t + "\"") + } + fmt.Println("\t}\n") + + fmt.Print("\t{\n") for _, t := range info.tests { fmt.Println("\t\t" + t) } diff --git a/evaldo/builtins_spreadsheet.go b/evaldo/builtins_spreadsheet.go index 0731454d..ca3c967e 100644 --- a/evaldo/builtins_spreadsheet.go +++ b/evaldo/builtins_spreadsheet.go @@ -19,7 +19,7 @@ import ( var Builtins_spreadsheet = map[string]*env.Builtin{ // Tests: - // equals { spreadsheet { "a" } { 1 2 } |type? } 'spreadsheet + // equal { spreadsheet { "a" } { 1 2 } |type? } 'spreadsheet // Args: // * columns // * data @@ -81,7 +81,7 @@ var Builtins_spreadsheet = map[string]*env.Builtin{ }, // Tests: - // equals { to-spreadsheet dict { "a" 1 "a" b } |type? } 'spreadsheet + // equal { to-spreadsheet list [ dict { "a" 1 } dict { "a" b } ] |type? } 'spreadsheet // Args: // * data "to-spreadsheet": { @@ -138,6 +138,10 @@ var Builtins_spreadsheet = map[string]*env.Builtin{ for key := range dict { k[key] = struct{}{} } + case env.Dict: + for key := range dict.Data { + k[key] = struct{}{} + } default: return MakeBuiltinError(ps, "List must contain only dicts", "to-spreadsheet") } @@ -158,8 +162,18 @@ var Builtins_spreadsheet = map[string]*env.Builtin{ } row[i] = data } + spr.AddRow(*env.NewSpreadsheetRow(row, spr)) + case env.Dict: + row := make([]any, len(keys)) + for i, key := range keys { + data, ok := dict.Data[key] + if !ok { + data = env.Void{} + } + row[i] = data + } + spr.AddRow(*env.NewSpreadsheetRow(row, spr)) } - spr.AddRow(*env.NewSpreadsheetRow(row, spr)) } return *spr @@ -194,7 +208,7 @@ var Builtins_spreadsheet = map[string]*env.Builtin{ // 2) A native that is a slice of SpreadsheetRows, like the value returned from `get-rows` // Tests: // equal { - // ref spreadsheet { "a" "b" } { 1 10 2 20 } :sheet + // ref spreadsheet { "a" "b" } { 1 10 2 20 } ::sheet // sheet .add-rows! [ 3 30 ] sheet .deref .length? // } 3 // Args: diff --git a/tests2/base.rye b/tests2/base.rye deleted file mode 100644 index fd489b3d..00000000 --- a/tests2/base.rye +++ /dev/null @@ -1,204 +0,0 @@ -section "base" "base text" { - - group "to-word" - "" - { } - { - equal { to-word "test" } 'test - error { to-word "123" } - } - - group "to-integer" - "" - { } - { - equal { to-integer "123" } 123 - ; equal { to-integer "123.4" } 123 - ; equal { to-integer "123.6" } 123 - ; equal { to-integer "123.4" } 123 - error { to-integer "abc" } - } - - group "to-decimal" - "" - { } - { - equal { to-decimal "123.4" } 123.4 - error { to-decimal "abc" } - } - - group "to-string" - "" - { } - { - equal { to-string 'test } "test" - equal { to-string 123 } "123" - equal { to-string 123.4 } "123.4000" - equal { to-string "test" } "test" - } - - group "to-char" - "" - { } - { - equal { to-char 42 } "*" - error { to-char "*" } - } - - group "to-block" - "" - { } - { - equal { list [ 1 2 3 ] |to-block |type? } 'block - equal { list [ 1 2 3 ] |to-block |first } 1 - } - - group "to-context" - "" - { } - { - equal { dict [ "a" 1 "b" 2 "c" 3 ] |to-context |type? } 'context - ; equal { dict [ "a" 1 ] |to-context do\in { a } } '1 - } - - group "is-string" - "" - { } - { - equal { is-string "test" } 1 - equal { is-string 'test } 0 - equal { is-string 123 } 0 - } - - group "is-integer" - "" - { } - { - equal { is-integer 123 } 1 - equal { is-integer 123.4 } 0 - equal { is-integer "123" } 0 - } - - group "is-decimal" - "" - { } - { - equal { is-decimal 123.0 } 1 - equal { is-decimal 123 } 0 - equal { is-decimal "123.4" } 0 - } - - group "is-number" - "" - { } - { - equal { is-number 123 } 1 - equal { is-number 123.4 } 1 - equal { is-number "123" } 0 - } - - group "to-uri" - "" - { } - { - equal { to-uri "https://example.com" } https://example.com - ; error { to-uri "not-uri" } - } - - group "to-file" - "" - { } - { - equal { to-file "example.txt" } %example.txt - error { to-file 123 } - } - - group "type?" - "" - { } - { - equal { type? "test" } 'string - equal { type? 123.4 } 'decimal - } - - group "types?" - "" - { } - { - equal { types? { "test" 123 } } { string integer } - } - - group "inc" - "" - { } - { - equal { inc 123 } 124 - error { inc "123" } - } - - group "is-positive" - "" - { } - { - equal { is-positive 123 } 1 - equal { is-positive -123 } 0 - error { is-positive "123" } - } - - group "is-zero" - "" - { } - { - equal { is-zero 0 } 1 - equal { is-zero 123 } 0 - error { is-zero "123" } - } - - group "inc!" - "" - { } - { - equal { a:: 123 inc! 'a a } 124 - error { inc! 123 } - } - - group "dec!" - "" - { } - { - equal { a:: 123 dec! 'a a } 122 - error { dec! 123 } - } - - group "change!" - "" - { } - { - equal { a:: 123 change! 333 'a a } 333 - equal { a:: 123 change! 124 'a } 1 - equal { a:: 123 change! 123 'a } 0 - } - - group "set!" - "" - { } - { - equal { set! { 123 234 } { a b } b } 234 - } - - group "embed" - "" - { } - { - equal { embed 101 "val {}" } "val 101" - } - - group "is-empty" - "" - { } - { - equal { { } .is-empty } 1 - } - -} - diff --git a/tests2/main.rye b/tests2/main.rye index 50698cc5..e2ce88ff 100644 --- a/tests2/main.rye +++ b/tests2/main.rye @@ -12,7 +12,7 @@ test-framework: context { section: fn { name descr code } { term/magenta term/bold print name term/reset\all print " " + descr , do code print "" } - group: fn { name descr args code } { inc! 'cnt term/blue prns " " + name term/reset\all , do code } + group: fn { name descr args code } { inc! 'cnt term/green prns " " + name term/reset\all , do code } error-: fn { test } { ; try { do\in root-ctx test } @@ -86,12 +86,12 @@ generate-menu: fn { menu } { generate-doc-file: fn { filename menu } { join { %tpl/header.html .read .replace "{{title}}" capitalize filename ; |replace "{{menu}}" generate-menu menu - capture-stdout { do\in docs-framework load to-file filename + ".rye" } + capture-stdout { do\in docs-framework load to-file filename + ".tests.rye" } read %tpl/footer.html } |write* to-file filename + ".html" } -menu: { "base" } +menu: { "base" "spreadsheet" } print-help: does { print `# Rye's simple testing tool @@ -123,7 +123,7 @@ test { errors: 0 for sections { .to-string .pass { term/bold , prns "\n#" , .to-upper .prns , print "#\n" } - |+ ".rye" |to-file |load |^check { "group does not exist!" } |do\in* test-framework |+ errors ::errors } + |+ ".tests.rye" |to-file |load |^check { "group does not exist!" } |do\in* test-framework |+ errors ::errors } print "" if ( length? sections ) > 1 { diff --git a/tests2/spreadsheet.rye b/tests2/spreadsheet.rye deleted file mode 100644 index 34143266..00000000 --- a/tests2/spreadsheet.rye +++ /dev/null @@ -1,81 +0,0 @@ -section "base" "base text" { - - group "spreadsheet" - "" - { } { - equals { spreadsheet { "a" } { 1 2 } |type? } 'spreadsheet - Args: - * columns - * data - } - - group "to-spreadsheet" - "" - { } { - equals { to-spreadsheet dict { "a" 1 "a" b } |type? } 'spreadsheet - Args: - * data - } - - group "add-rows" - "" - { } { - equal { - ref spreadsheet { "a" "b" } { 1 10 2 20 } :sheet - sheet .add-rows! [ 3 30 ] sheet .deref .length? - } 3 - Args: - * sheet -the sheet that is getting rows added to it - * rows - a block containing one or more rows worth of values, or a SpreadsheetRow Native value - } - - group "add-rows!" - "" - { } { - equal { - ref spreadsheet { "a" "b" } { 1 10 2 20 } :sheet - sheet .add-rows! [ 3 30 ] sheet .deref .length? - } 3 - Args: - * sheet - the reference to the sheet that is getting rows added to it - * rows - a block containing one or more rows worth of values, or a SpreadsheetRow Native value - Tags: #spreasheet #mutation - } - - group "update-row!" - "" - { } { - equal { - spr1: ref spreadsheet { "a" "b" } { 1 10 2 20 } - spr1 .update-row! 1 dict [ "a" 111 ] - spr1 .deref .A1 - } 111 - equal { - spr1: ref spreadsheet { "a" "b" } { 1 10 2 20 } - incrA: fn { row } { row -> "a" + 1 :new-a dict { "a" new-a } } - spr1 .update-row! 1 dict incrA - spr1 .deref .A1 - } 11 - Args: - * sheet-ref - A ref to a spreadsheet - * idx - the index of the row to update, 1-based - * updater - One of either a function, a dict, or a Spreadsheet Row - Tags: #spreadsheet #mutation - } - - group "remove-row!" - "" - { } { - equal { - spr1: spreadsheet { "a" "b" } { 1 10 2 20 } - spr1 .remove-row! 1 - spr1 .deref .A1 - } 2 - Args: - * sheet-ref - * row-idx - Index of row to remove, 1-based - Tags: #spreadsheet #mutation - } - - } -