Skip to content

Commit 88034b9

Browse files
committed
Added tests, rockspec and Travis integration
1 parent e561075 commit 88034b9

File tree

5 files changed

+124
-3
lines changed

5 files changed

+124
-3
lines changed

.travis.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: erlang
2+
3+
env:
4+
- LUA=""
5+
- LUA="luajit"
6+
7+
branches:
8+
only:
9+
- master
10+
11+
install:
12+
- sudo apt-get install luajit
13+
- sudo apt-get install luarocks
14+
- sudo luarocks install luafilesystem
15+
- sudo luarocks install busted
16+
17+
script: "busted spec"
18+

README.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Classic
1+
# Classic [![Build Status](https://api.travis-ci.org/thefosk/classic.png)](https://travis-ci.org/thefosk/classic)
22

33
A tiny class module for Lua. Attempts to stay simple and provide decent
44
performance by avoiding unnecessary over-abstraction.
@@ -48,7 +48,7 @@ end
4848
local p = Point(10, 20)
4949
print(p:is(Object)) -- true
5050
print(p:is(Point)) -- true
51-
print(p:is(Rect)) -- false
51+
print(p:is(Rect)) -- false
5252
```
5353

5454
### Using mixins
@@ -97,6 +97,27 @@ function Point:__tostring()
9797
end
9898
```
9999

100+
### Returning constructor errors
101+
```lua
102+
Rect = Point:extend()
103+
104+
function Rect:new(x, y, width, height)
105+
if width > 100 then
106+
return nil, "Width can't be greater than 100"
107+
end
108+
109+
Rect.super.new(self, x, y)
110+
self.width = width or 0
111+
self.height = height or 0
112+
end
113+
114+
115+
local rect, err = Rect(10, 20, 200, 200)
116+
if not rect then
117+
print(err) -- The err variable contains the error message
118+
end
119+
```
120+
100121

101122
## License
102123

classic-0.1-1.rockspec

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package = "classic"
2+
version = "0.1-1"
3+
source = {
4+
url = "git://github.com/rxi/classic",
5+
branch = "master"
6+
}
7+
description = {
8+
summary = "Tiny class module for Lua",
9+
detailed = [[
10+
A tiny class module for Lua. Attempts to stay simple and provide decent performance by avoiding unnecessary over-abstraction.
11+
]],
12+
homepage = "https://github.com/rxi/classic",
13+
license = "MIT"
14+
}
15+
dependencies = {
16+
"lua >= 5.1"
17+
}
18+
build = {
19+
type = "builtin",
20+
modules = {
21+
classic = "src/classic.lua"
22+
}
23+
}

spec/classic_spec.lua

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Object = require "classic"
2+
3+
-- BaseClass provides a say() method
4+
local BaseClass = Object:extend()
5+
function BaseClass:new(name)
6+
self._name = name
7+
end
8+
function BaseClass:get_name()
9+
return self._name
10+
end
11+
function BaseClass.say_something()
12+
return "something"
13+
end
14+
15+
-- ClassOne extends BaseClass
16+
local ClassOne = BaseClass:extend()
17+
function ClassOne:new(name)
18+
ClassOne.super.new(self, name)
19+
end
20+
function ClassOne.say_something()
21+
return "something better"
22+
end
23+
24+
-- ClassTwo extends BaseClass
25+
local ClassTwo = BaseClass:extend()
26+
function ClassTwo:new(name)
27+
if name == "wrong" then
28+
error({message = "Wrong value"})
29+
end
30+
ClassTwo.super.new(self, name)
31+
end
32+
33+
describe("classic #classic", function()
34+
describe("Base tests", function()
35+
36+
it("Constructors should work", function()
37+
local class_one = ClassOne("Mark")
38+
local class_two = ClassTwo("John")
39+
40+
assert.are.same("Mark", class_one:get_name())
41+
assert.are.same("John", class_two:get_name())
42+
end)
43+
it("Static method should work", function()
44+
local class_one = ClassOne("Mark")
45+
local class_two = ClassTwo("John")
46+
47+
assert.are.same("something better", class_one:say_something())
48+
assert.are.same("something", class_two:say_something())
49+
end)
50+
it("Constructor returns error", function()
51+
local status, res = pcall(ClassTwo, "wrong")
52+
53+
assert.falsy(status)
54+
assert.truthy(res)
55+
assert.are.same("Wrong value", res.message)
56+
end)
57+
58+
end)
59+
end)

classic.lua src/classic.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ function Object:__call(...)
6565
end
6666

6767

68-
return Object
68+
return Object

0 commit comments

Comments
 (0)