Skip to content

Commit

Permalink
port to Teal
Browse files Browse the repository at this point in the history
catwell committed Dec 30, 2024

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent 77d66c5 commit b6ff339
Showing 6 changed files with 1,057 additions and 398 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Copyright (C) 2012-2013 by Moodstocks SAS
Copyright (C) 2014-2022 by Pierre Chapuis
Copyright (C) from 2014 by Pierre Chapuis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

## Presentation

This is a small Lua test helper with no hard dependency.
This is a small Teal / Lua test helper with no hard dependency.

## Alternatives

@@ -50,6 +50,12 @@ Multiplication ...x. FAILED (3 OK, 1 KO)
Squares .......... OK (10 OK, 0 KO)
```

## Changes

### 3.0

cwtest version 3 was rewritten in [Teal](https://github.com/teal-language/tl). Teal compiles down to Lua so you can still use it in Lua. It has the same interface with one major caveat: `failures`, `successes` and `suite` are now method accessors, i.e. you must use `T:successes()` instead of `T.successes`.

## Details

### do/end block
@@ -163,6 +169,6 @@ You can find an example of this
## Copyright

- Copyright (c) 2012-2013 Moodstocks SAS
- Copyright (c) 2014-2022 Pierre Chapuis
- Copyright (c) from 2014 Pierre Chapuis

Some code borrowed from [Penlight](https://github.com/lunarmodules/Penlight), released under the MIT license by Steve Donovan and David Manura.
Some code adapted from [Penlight](https://github.com/lunarmodules/Penlight), released under the MIT license by Steve Donovan and David Manura.
847 changes: 478 additions & 369 deletions cwtest.lua

Large diffs are not rendered by default.

44 changes: 27 additions & 17 deletions cwtest.test.lua → cwtest.test.tl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
local cwtest = require "cwtest"

local T = cwtest.new()
local interface ExtendedTester is cwtest.Tester
last_success: function(self: cwtest.Tester, tpl: string, ...: any):(boolean)
last_failure: function(self: cwtest.Tester, tpl: string, ...: any):(boolean)
end

local T = cwtest.new() as ExtendedTester

local C = cwtest.new() -- tested instance
C.printf = function() end
@@ -10,7 +15,7 @@ local short_src = debug.getinfo(1).short_src
local exp_got_tpl = "\n expected: %s\n got: %s"
local pw = assert(cwtest.pretty_write)

local _line = function(status, before, tpl, ...)
local _line = function(status: string, before: integer, tpl: string, ...: any): string
assert(
(type(status) == "string") and
(type(before) == "number") and
@@ -23,10 +28,11 @@ local _line = function(status, before, tpl, ...)
)
end

T.last_success = function(self, tpl, ...)
T.last_success = function(self: cwtest.Tester, tpl: string, ...: any): boolean
local l = _line("OK", 1, tpl, ...)
local s = C.successes[#C.successes]
local r
local successes = C:successes()
local s = successes[#successes]
local r: boolean
if s == l then
r = self.pass_tpl(self, exp_got_tpl, l, s)
else
@@ -35,10 +41,11 @@ T.last_success = function(self, tpl, ...)
return r
end

T.last_failure = function(self, tpl, ...)
T.last_failure = function(self: cwtest.Tester, tpl: string, ...: any): boolean
local l = _line("KO", 1, tpl, ...)
local s = C.failures[#C.failures]
local r
local failures = C:failures()
local s = failures[#failures]
local r: boolean
if s == l then
r = self.pass_tpl(self, exp_got_tpl, l, s)
else
@@ -47,10 +54,11 @@ T.last_failure = function(self, tpl, ...)
return r
end


T:start("successes", 26); do
C:start("successes")
T:eq( C.successes, {} )
T:eq( C.failures, {} )
T:eq( C:successes(), {} )
T:eq( C:failures(), {} )
T:yes(C:yes( 0 ))
T:last_success( " (assertion)" )
T:yes(C:yes( true, " foo" ))
@@ -63,7 +71,9 @@ T:start("successes", 26); do
T:last_success( " foo bar 42" )
T:yes(C:eq( 3*2, 6 ))
T:last_success( exp_got_tpl, 6, 6 )
local _i,_o = {1, 2, a = {b = 42}}, {[2] = 2, a = {b = 42}, [1] = 1}
local _i: any
local _o: any
_i, _o = {1, 2, a = {b = 42}}, {[2] = 2, a = {b = 42}, [1] = 1}
T:yes(C:eq( _i, _o ))
T:last_success( exp_got_tpl, pw(_o), pw(_i) )
_i, _o = {2, {x = 2.5}, 3}, {3, 2, {x = 2.5}}
@@ -75,14 +85,14 @@ T:start("successes", 26); do
T:last_success( ": error [[foo]] caught" )
T:yes(C:err( function() error("foo", 0) end ))
T:last_success( ": error caught" )
T:eq( C.failures, {} )
T:eq( C:failures(), {} )
T:eq(C:done(), true)
end; T:done()

T:start("failures", 28); do
C:start("failures")
T:eq( C.successes, {} )
T:eq( C.failures, {} )
T:eq( C:successes(), {} )
T:eq( C:failures(), {} )
T:no(C:yes( false ))
T:last_failure( " (assertion)" )
T:no(C:yes( nil, " foo" ))
@@ -105,11 +115,11 @@ T:start("failures", 28); do
T:last_failure( " ({} == {})" )
T:no(C:err( function() error("foo", 0) end, "bar" ))
T:last_failure( "\n expected error: bar\n got error: foo" )
T:no(C:err( function() return 42 end, "foo" ))
T:no(C:err( function():(integer) return 42 end, "foo" ))
T:last_failure( "\n expected error: foo\n got: %s", pw({42}) )
T:no(C:err( function() return 42 end ))
T:no(C:err( function():(integer) return 42 end ))
T:last_failure( ": expected error, got %s", pw({42}) )
T:eq( C.successes, {} )
T:eq( C:successes(), {} )
T:eq(C:done(), false)
end; T:done()

538 changes: 538 additions & 0 deletions cwtest.tl

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions rockspec/cwtest-scm-1.rockspec
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ source = {
description = {
summary = "Test helper",
detailed = [[
cwtest is a tiny Lua test helper.
cwtest is a tiny Teal / Lua test helper.
]],
homepage = "https://github.com/catwell/cwtest",
license = "MIT/X11",
@@ -19,12 +19,8 @@ description = {
dependencies = { "lua >= 5.1" }

build = {
type = "none",
install = { lua = { cwtest = "cwtest.lua" } },
type = "builtin",
modules = { cwtest = "cwtest.lua" },
install = { lua = { cwtest = "cwtest.tl" } },
copy_directories = {},
}

test = {
type = "command",
script = "cwtest.test.lua",
}

0 comments on commit b6ff339

Please sign in to comment.