Skip to content

Commit

Permalink
coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdugne committed Jun 14, 2017
1 parent 49503f7 commit a64095f
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 24 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ stds.cherry = {
std = 'min+corona+cherry'
ignore = {'212'}
files['test'] = {std = '+busted'}
exclude_files = {'lua_install/*'}
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ install:
- ./install-rocks

script:
- luacheck **/*.lua
- luacheck .
- ./test/run travis

after_success:
Expand Down
4 changes: 1 addition & 3 deletions libs/analytics.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--------------------------------------------------------------------------------

local http = require 'http'
local http = _G.http or require 'http'

local ANALYTICS_URL = "http://www.google-analytics.com/collect"

Expand Down Expand Up @@ -38,10 +38,8 @@ local function event(category, action, label)
data = data .. "v=" .. _G.analyticsParams.version
data = data .. "&tid=" .. _G.analyticsParams.trackingId
data = data .. "&cid=" .. _G.analyticsParams.profileId

data = data .. "&t=" .. "event"
data = data .. "&an=" .. _G.analyticsParams.AppName

data = data .. "&ec=" .. category
data = data .. "&ea=" .. action

Expand Down
3 changes: 1 addition & 2 deletions libs/animation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ local animation = {}
--------------------------------------------------------

function animation.rotateBackAndForth(object, angle, time)

local initialRotation = object.rotation

local back = function()
transition.to(object, {
rotation = initialRotation,
onComplete = animation.rotateBackAndForth(object, -angle, time),
onComplete = function() animation.rotateBackAndForth(object, -angle, time) end,
time = time
})
end
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
## lua
## Cherry.lua

[![Build Status](https://travis-ci.org/chrisdugne/svg?branch=master)](https://travis-ci.org/chrisdugne/cherry)
[![Travis](https://img.shields.io/travis/chridugne/cherry.svg)]()
[![codecov](https://codecov.io/gh/chrisdugne/cherry/branch/master/graph/badge.svg)](https://codecov.io/gh/chrisdugne/cherry)
[![License](https://img.shields.io/badge/License-BSD%203--Clause-green.svg?colorB=3cc712)](LICENSE)
![version](https://img.shields.io/github/tag/chrisdugne/svg?colorB=3cc712)
[![GitHub release](https://img.shields.io/github/release/chrisdugne/cherry.svg)]()

Cherry is an open source starter for `CoronaSDK App` to help building your game.
Cherry is a starter for `CoronaSDK App` to help building your game.

- [Introduction](#introduction-to-cherry)
- [Usage](#usage)
Expand All @@ -17,7 +17,7 @@ Cherry is an open source starter for `CoronaSDK App` to help building your game.
- [Third Parties](#third-parties)
- [Games using Cherry](#games-using-cherry)

![cherry](/docs/png)
![cherry](/docs/cherry.png)

## Introduction to Cherry

Expand Down
3 changes: 2 additions & 1 deletion test/.luacov
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ return {
-- the .lua extension)
['exclude'] = {
'mocks',
'spec'
'mocks',
'tprint'
},
}
14 changes: 14 additions & 0 deletions test/_global.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
expose('[setting up tests Globals]', function()
require('tprint')

_G.sleep = function(n)
local t = os.clock()
while os.clock() - t <= n do
-- nothing
end
end

_G.display = require('mocks.display')
_G.transition = require('mocks.transition')
_G.App = require('mocks.app')
_G.network = require('mocks.network')
_G.http = require('mocks.http')

_G.easing = {}
_G.timer = {performWithDelay = function(time, func) func() end}
end)
4 changes: 3 additions & 1 deletion test/mocks/app.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local App = {
display = display.newGroup(),
start = function () end,
start = function () end,
width = 640,
height = 1280,
}

return App
3 changes: 2 additions & 1 deletion test/mocks/display.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ local function newDisplayObject()
yReference = 0,
yScale = 1,
referencePoint = display.TopLeftReferencePoint,
eventListeners = {}
eventListeners = {},
onCompleteCounts = 1 --- cherry default transition testing: calling onComplete once
}

_displayObject.setFillColor = function() return end
Expand Down
6 changes: 6 additions & 0 deletions test/mocks/http.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local http = {
post = function (url, data, next, _type)
end
}

return http
6 changes: 6 additions & 0 deletions test/mocks/network.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local network = {
request = function (url, method, next, params)
end
}

return network
6 changes: 6 additions & 0 deletions test/mocks/transition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@ return {
for k,v in pairs(properties) do
object[k] = v
end
if(object.onComplete) then
object.onCompleteCounts = (object.onCompleteCounts or 0) - 1
if(object.onCompleteCounts >= 0) then
object.onComplete()
end
end
end
}
42 changes: 42 additions & 0 deletions test/spec/libs/analytics.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local analytics = require 'analytics'

describe('[analytics]', function()
it('> should set _G.analyticsParams', function()
analytics.init('plop', 'plup', 'plip', 'ploup', 'plep')
assert.are.equal( _G.analyticsParams.version, 'plop')
assert.are.equal( _G.analyticsParams.trackingId, 'plup')
assert.are.equal( _G.analyticsParams.profileId, 'plip')
assert.are.equal( _G.analyticsParams.AppName, 'ploup')
assert.are.equal( _G.analyticsParams.AppVersion, 'plep')
end)

it('> should post a page view', function()
stub(_G.http, 'post')

analytics.init('plop', 'plup', 'plip', 'ploup', 'plep')
analytics.pageview('page')

assert.stub(_G.http.post).was.called(1)
assert.stub(_G.http.post).was.called_with(
'http://www.google-analytics.com/collect',
'v=plop&tid=plup&cid=plip&t=Appview&an=ploup&av=plep&cd=page',
nil,
'urlencoded'
)
end)

it('> should post an event', function()
stub(_G.http, 'post')

analytics.init('plop', 'plup', 'plip', 'ploup', 'plep')
analytics.event('cat', 'act', 'lab')

assert.stub(_G.http.post).was.called(1)
assert.stub(_G.http.post).was.called_with(
'http://www.google-analytics.com/collect',
'v=plop&tid=plup&cid=plip&t=event&an=ploup&ec=cat&ea=act&el=lab',
nil,
'urlencoded'
)
end)
end)
76 changes: 76 additions & 0 deletions test/spec/libs/animation.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
local animation = require 'animation'

describe('[animation]', function()
it('> rotateBackAndForth', function()
local foo = display.newImage(App.display)
assert.are.equal(foo.rotation, 0)

foo.onCompleteCounts = 0
animation.rotateBackAndForth(foo, 45, 200)
assert.are.equal(foo.rotation, 45)

foo.rotation = 0
foo.onCompleteCounts = 1
animation.rotateBackAndForth(foo, 45, 200)
assert.are.equal(foo.rotation, 0)
end)

it('> easeDisplay', function()
local foo = display.newImage(App.display)
foo.xScale = 0
foo.yScale = 0

animation.easeDisplay(foo)
assert.are.equal(foo.xScale, 1)
assert.are.equal(foo.yScale, 1)

animation.easeDisplay(foo, 2)
assert.are.equal(foo.xScale, 2)
assert.are.equal(foo.yScale, 2)
end)

it('> bounce', function()
local foo = display.newImage(App.display)
foo.xScale = 0
foo.yScale = 0

animation.bounce(foo)
assert.are.equal(foo.xScale, 1)
assert.are.equal(foo.yScale, 1)

animation.bounce(foo, 2)
assert.are.equal(foo.xScale, 2)
assert.are.equal(foo.yScale, 2)
end)

it('> grow', function()
local foo = display.newImage(App.display)
local action = spy.new(function() end)

animation.grow(foo, 0, 350, action)

assert.are.equal(foo.xScale, 1)
assert.are.equal(foo.yScale, 1)
assert.spy(action).was.called(1)
end)

it('> easeHide', function()
local foo = display.newImage(App.display)
local action = spy.new(function() end)

animation.easeHide(foo, action)

assert.are.equal(foo.xScale, 0.01)
assert.are.equal(foo.yScale, 0.01)
assert.spy(action).was.called(1)
end)

it('> fadeIn', function()
local foo = display.newImage(App.display)
foo.alpha = 0

animation.fadeIn(foo)

assert.are.equal(foo.alpha, 1)
end)
end)
120 changes: 110 additions & 10 deletions test/spec/libs/gesture.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,117 @@
local gesture = require 'gesture'

describe('[gesture]', function()
it('> onTouch', function()
local foo = display.newImage(App.display)
local action = spy.new(function() end)
describe('[onTouch]', function()
it('> phase.began: action is not called, opacity becomes 0.8', function()
local foo = display.newImage(App.display)
local action = spy.new(function() end)
assert.are.equal(foo.alpha, 1)

gesture.onTouch(foo, action)
gesture.onTouch(foo, action)

foo:dispatchEvent({
name = 'touch',
phase = 'ended'
})
foo:dispatchEvent({
name = 'touch',
phase = 'began'
})

assert.spy(action).was.called(1)
end)
assert.are.equal(foo.alpha, 0.8)
assert.spy(action).was.called(0)
end)

it('> phase.ended: action is called, opacity is back to 1', function()
local foo = display.newImage(App.display)
local action = spy.new(function() end)

gesture.onTouch(foo, action)

foo:dispatchEvent({
name = 'touch',
phase = 'ended'
})

assert.spy(action).was.called(1)
assert.are.equal(foo.alpha, 1)
end)

it('> should override previous action', function()
local foo = display.newImage(App.display)
local action1 = spy.new(function() end)
local action2 = spy.new(function() end)

gesture.onTouch(foo, action1)
gesture.onTouch(foo, action2)

foo:dispatchEvent({
name = 'touch',
phase = 'ended'
})

assert.spy(action1).was.called(0)
assert.spy(action2).was.called(1)
end)
end)

describe('[onTap]', function()
it('> phase.began: action is called', function()
local foo = display.newImage(App.display)
local action = spy.new(function() end)

gesture.onTap(foo, action)

foo:dispatchEvent({
name = 'touch',
phase = 'began'
})

assert.spy(action).was.called(1)
end)

it('> phase.ended: action is not called', function()
local foo = display.newImage(App.display)
local action = spy.new(function() end)

gesture.onTap(foo, action)

foo:dispatchEvent({
name = 'touch',
phase = 'ended'
})

assert.spy(action).was.called(0)
end)

it('> should override previous action', function()
local foo = display.newImage(App.display)
local action1 = spy.new(function() end)
local action2 = spy.new(function() end)

gesture.onTap(foo, action1)
gesture.onTap(foo, action2)

foo:dispatchEvent({
name = 'touch',
phase = 'began'
})

assert.spy(action1).was.called(0)
assert.spy(action2).was.called(1)
end)
end)

describe('[disabledTouch]', function()
it('> phase.began: action is called', function()
local foo = display.newImage(App.display)
local action = spy.new(function() end)

gesture.onTap(foo, action)
gesture.disabledTouch(foo)

foo:dispatchEvent({
name = 'touch',
phase = 'began'
})

assert.spy(action).was.called(0)
end)
end)
end)

0 comments on commit a64095f

Please sign in to comment.