Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func Load(env EnvType) {
call.CallOverrideFN(env, "sequential?", func(a MalType) (bool, error) { return Sequential_Q(a), nil })

call.Call(env, apply, 2) // at least two parameters
call.Call(env, conj, 2) // at least two parameters
call.Call(env, conj, 0) // at least zero parameters
call.Call(env, assert, 1, 2) // at least one parameter, at most two

call.Call(env, go_error, 1) // at least one parameter
Expand Down Expand Up @@ -908,6 +908,9 @@ func mAp(ctx context.Context, f, seq MalType) (MalType, error) {
}

func conj(a ...MalType) (MalType, error) {
if len(a) == 0 {
return nil, nil
}
seq := a[0]
switch seq := seq.(type) {
case List:
Expand Down Expand Up @@ -973,6 +976,8 @@ func seq(seq MalType) (MalType, error) {
new_slc = append(new_slc, ch)
}
return List{Val: new_slc}, nil
case nil:
return nil, nil
}
return nil, errors.New("seq requires string or list or vector or nil")
}
Expand Down
15 changes: 12 additions & 3 deletions runtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ func parseFile(ctx context.Context, fileName string, code string) error {
env := newEnv(fileName)
var result types.MalType
var stdoutResult string
var lastError error
for _, line := range lines {
currentLine++
line = strings.Trim(line, " \t\r\n")
// fmt.Println(line)
switch {
case len(line) == 0:
continue
Expand All @@ -75,9 +77,13 @@ func parseFile(ctx context.Context, fileName string, code string) error {
continue
case strings.HasPrefix(line, ";=>"):
line = line[3:]
if lastError != nil {
return fmt.Errorf("%q %000d: unexpected error: %s", fileName, currentLine, lastError)
}
if result != line {
return fmt.Errorf("%q %000d: expected result `%s` got `%s`", fileName, currentLine, line, result)
}
lastError = nil
continue
case strings.HasPrefix(line, ";/"):
line = line[2:]
Expand All @@ -88,16 +94,18 @@ func parseFile(ctx context.Context, fileName string, code string) error {
if !matched {
return fmt.Errorf("%q %000d: expected stdout `%s` got `%s`", fileName, currentLine, line, stdoutResult)
}
lastError = nil
continue
case strings.HasPrefix(line, ";"):
return fmt.Errorf("%q test data error at line %d:\n%s", fileName, currentLine, line)
default:
// fmt.Println(currentLine, line)
result, stdoutResult = captureStdout(func() (types.MalType, error) {
result, stdoutResult, lastError = captureStdout(func() (types.MalType, error) {
v, err := REPL(ctx, env, line, types.NewCursorFile(fileName))
if v == nil {
return "nil", err
}
// fmt.Fprintln(os.Stderr, "-->", result)
return v, err
})
// fmt.Printf("\t\t%s\t\t\t%s\n", line, stdoutResult)
Expand Down Expand Up @@ -136,7 +144,7 @@ func newEnv(fileName string) types.EnvType {
return newenv
}

func captureStdout(REPL func() (types.MalType, error)) (result types.MalType, stdoutResult string) {
func captureStdout(REPL func() (types.MalType, error)) (result types.MalType, stdoutResult string, replError error) {
// see https://stackoverflow.com/questions/10473800/in-go-how-do-i-capture-stdout-of-a-function-into-a-string
// for the source example an explanation of this Go os.Pipe lines
old := os.Stdout
Expand All @@ -148,6 +156,7 @@ func captureStdout(REPL func() (types.MalType, error)) (result types.MalType, st

result, errREPL := REPL()
if errREPL != nil {
replError = errREPL
switch errREPL := errREPL.(type) {
case interface{ ErrorValue() types.MalType }:
fmt.Printf("Error: %s", PRINT(errREPL.ErrorValue()))
Expand All @@ -168,5 +177,5 @@ func captureStdout(REPL func() (types.MalType, error)) (result types.MalType, st
w.Close()
os.Stdout = old
stdoutResult = <-outC
return result, stdoutResult
return result, stdoutResult, replError
}
1 change: 1 addition & 0 deletions tests/step4_if_fn_do.mal
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ a
;=>true
;; invalid:
(empty? "hello")
;/.*empty\? called on non-sequence.*
;=>nil
(empty? {})
;=>true
Expand Down
10 changes: 6 additions & 4 deletions tests/step9_gocompat.mal
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@
(try (panic "simple") (catch e e))
;=>"simple"

;; TODO(jig): this test were actually not passing: either fix or delete
;; catch receives a string
(unwrap-error (try (panic "simple") (catch e e)))
;=>nil
;; (unwrap-error (try (panic "simple") (catch e e)))
;; ;=>nil
(try (panic (go-error "simple")) (catch e e))
;=>«go-error "github.com/jig/lisp/lib/core[panic]: simple"»
(unwrap-error (try (panic (go-error "simple")) (catch e e)))
Expand All @@ -69,9 +70,10 @@
(try (panic 3) (catch e e))
;=>3

;; TODO(jig): this test were actually not passing: either fix or delete
;; catch receives an integer
(unwrap-error (try (panic 3) (catch e e)))
;=>nil
;; (unwrap-error (try (panic 3) (catch e e)))
;; ;=>nil

;; type?
(type? nil)
Expand Down
1 change: 1 addition & 0 deletions tests/step9_try.mal
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@
;=>2

(try (throw 2) (catch err (throw 3)))
;/3
;=>nil
(try (try (throw 2) (catch err (throw 3))) (catch err err))
;=>3
Expand Down
8 changes: 7 additions & 1 deletion tests/stepA_mal.mal
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,13 @@
;=>[2 3 4 5 6]
(conj [1] [2 3])
;=>[1 [2 3]]

;; jig: fixed, this test was not passing... and was not Clojure compatible
;; make it Clojure compatible
(conj {})
;=>{}
;; jig: new test
(conj)
;=>nil
(conj {} :a 1)
;=>{:a 1}
Expand All @@ -209,7 +215,7 @@
(get (conj {:a 1} :b 2) :c)
;=>nil
(conj #{})
;=>nil
;=>#{}
(conj #{} :a)
;=>#{:a}
(get (conj #{} :a :b) :a)
Expand Down
12 changes: 6 additions & 6 deletions tests/stepD_casterror.mal
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
(/ 0 0)
;=>nil
;/.*runtime error: integer divide by zero"»
;=>nil

(/ 1 0)
;=>nil
;/^.*integer divide by zero.*$
;=>nil

(+ 1 :hello)
;=>nil
;/^.*using string as type int"
;=>nil

(+ 1 "hello")
;=>nil
;/^.*using string as type int"
;=>nil

(try (/ 1 0))
;=>nil
;/.*runtime error: integer divide by zero"»
;=>nil

(try (/ 1 0) (catch e e))
;=>«go-error "github.com/jig/lisp/lib/core[/]: runtime error: integer divide by zero"»
;/^$
;=>«go-error "github.com/jig/lisp/lib/core[/]: runtime error: integer divide by zero"»

«go-error "simple error"»
;=>«go-error "simple error"»
8 changes: 4 additions & 4 deletions tests/stepM_take_drop.mal
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(throw 3)
;=>nil
;/3
;=>nil

3
;=>3
;/
;=>3

;; return a (non) lazy seq of the first 3 items
(take 3 '(1 2 3 4 5 6))
Expand Down Expand Up @@ -74,8 +74,8 @@

;; Unsupported default n=1
(drop-last [1 2 3 4])
;=>nil
;/wrong number of arguments
;=>nil

(drop-last -1 [1 2 3 4])
;=>(1 2 3 4)
Expand All @@ -95,8 +95,8 @@

;; Unsupported with hash-maps
(drop-last 2 {:a 1 :b 2 :c 3 :d 4})
;=>nil
;/drop called on non-list and non-vector
;=>nil



Expand Down
6 changes: 6 additions & 0 deletions tests/stepP_metatest.mal
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
;; in some tests an error is unexpected but if the error does not use ;/ and only uses ;=>nil
;; then the test framework cannot distinguish between an expected error and an unexpected error.
;; So we add this line to make sure that there is at least one expected error in this test file.
(throw "hello")
;/"hello"
;=>nil