From 3cc7c6374146b3302edd794b7ea7fce8348e5928 Mon Sep 17 00:00:00 2001 From: thefosk Date: Thu, 18 Dec 2014 15:52:51 -0800 Subject: [PATCH 1/2] Added tests, rockspec and Travis integration --- .travis.yml | 18 +++++++++++ README.md | 5 ++- classic-0.1-1.rockspec | 23 +++++++++++++ spec/classic_spec.lua | 59 ++++++++++++++++++++++++++++++++++ classic.lua => src/classic.lua | 2 +- 5 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 .travis.yml create mode 100644 classic-0.1-1.rockspec create mode 100644 spec/classic_spec.lua rename classic.lua => src/classic.lua (98%) diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a47cb55 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +language: erlang + +env: + - LUA="" + - LUA="luajit" + +branches: + only: + - master + +install: + - sudo apt-get install luajit + - sudo apt-get install luarocks + - sudo luarocks install luafilesystem + - sudo luarocks install busted + +script: "busted spec" + diff --git a/README.md b/README.md index 7f1e625..b64dd3a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Classic +# Classic [![Build Status](https://api.travis-ci.org/thefosk/classic.png)](https://travis-ci.org/thefosk/classic) A tiny class module for Lua. Attempts to stay simple and provide decent performance by avoiding unnecessary over-abstraction. @@ -48,7 +48,7 @@ end local p = Point(10, 20) print(p:is(Object)) -- true print(p:is(Point)) -- true -print(p:is(Rect)) -- false +print(p:is(Rect)) -- false ``` ### Using mixins @@ -102,4 +102,3 @@ end This module is free software; you can redistribute it and/or modify it under the terms of the MIT license. See [LICENSE](LICENSE) for details. - diff --git a/classic-0.1-1.rockspec b/classic-0.1-1.rockspec new file mode 100644 index 0000000..61ef035 --- /dev/null +++ b/classic-0.1-1.rockspec @@ -0,0 +1,23 @@ +package = "classic" +version = "0.1-1" +source = { + url = "git://github.com/rxi/classic", + branch = "master" +} +description = { + summary = "Tiny class module for Lua", + detailed = [[ + A tiny class module for Lua. Attempts to stay simple and provide decent performance by avoiding unnecessary over-abstraction. + ]], + homepage = "https://github.com/rxi/classic", + license = "MIT" +} +dependencies = { + "lua >= 5.1" +} +build = { + type = "builtin", + modules = { + classic = "src/classic.lua" + } +} diff --git a/spec/classic_spec.lua b/spec/classic_spec.lua new file mode 100644 index 0000000..9a0e269 --- /dev/null +++ b/spec/classic_spec.lua @@ -0,0 +1,59 @@ +Object = require "classic" + +-- BaseClass provides a say() method +local BaseClass = Object:extend() +function BaseClass:new(name) + self._name = name +end +function BaseClass:getName() + return self._name +end +function BaseClass.getSomething() + return "something" +end + +-- ClassOne extends BaseClass +local ClassOne = BaseClass:extend() +function ClassOne:new(name) + ClassOne.super.new(self, name) +end +function ClassOne.getSomething() + return "something better" +end + +-- ClassTwo extends BaseClass +local ClassTwo = BaseClass:extend() +function ClassTwo:new(name) + if name == "wrong" then + error("Wrong value") + end + ClassTwo.super.new(self, name) +end + +describe("classic #classic", function() + describe("Base tests", function() + + it("Constructors should work", function() + local classOne = ClassOne("Mark") + local classTwo = ClassTwo("John") + + assert.are.same("Mark", classOne:getName()) + assert.are.same("John", classTwo:getName()) + end) + it("Static method should work", function() + local classOne = ClassOne("Mark") + local classTwo = ClassTwo("John") + + assert.are.same("something better", classOne:getSomething()) + assert.are.same("something", classTwo:getSomething()) + end) + it("Constructor returns error", function() + local status, res = pcall(ClassTwo, "wrong") + + assert.falsy(status) + assert.truthy(res) + assert.are.same("Wrong value", string.sub(res, string.len(res) - 10)) + end) + + end) +end) \ No newline at end of file diff --git a/classic.lua b/src/classic.lua similarity index 98% rename from classic.lua rename to src/classic.lua index cbd6f81..6cefdb7 100644 --- a/classic.lua +++ b/src/classic.lua @@ -65,4 +65,4 @@ function Object:__call(...) end -return Object +return Object \ No newline at end of file From 1041afa251f644afb58028badf4be4bee74eee7e Mon Sep 17 00:00:00 2001 From: thefosk Date: Wed, 11 Mar 2015 14:26:56 -0700 Subject: [PATCH 2/2] Making sure that no arguments are being passed to the static methods --- spec/classic_spec.lua | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/spec/classic_spec.lua b/spec/classic_spec.lua index 9a0e269..e073198 100644 --- a/spec/classic_spec.lua +++ b/spec/classic_spec.lua @@ -44,8 +44,24 @@ describe("classic #classic", function() local classOne = ClassOne("Mark") local classTwo = ClassTwo("John") - assert.are.same("something better", classOne:getSomething()) - assert.are.same("something", classTwo:getSomething()) + spy.on(classOne, "getSomething") + spy.on(classTwo, "getSomething") + + assert.are.same("something better", classOne.getSomething()) + assert.are.same("something", classTwo.getSomething()) + + -- Make sure no arguments have been passed + assert.spy(classOne.getSomething).was_called_with() + assert.spy(classTwo.getSomething).was_called_with() + + assert.are.same("something better", classOne.getSomething("hello")) + assert.spy(classOne.getSomething).was_called_with("hello") + + -- Removing the spies + finally(function() + classOne.getSomething:revert() + classTwo.getSomething:revert() + end) end) it("Constructor returns error", function() local status, res = pcall(ClassTwo, "wrong")