Skip to content

Commit

Permalink
fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
akira-cn committed May 15, 2018
1 parent 11713ae commit 5cabfda
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 26 deletions.
32 changes: 13 additions & 19 deletions example/node/maketest.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
const {createCanvas} = require('canvas')
const fs = require('fs')

const Label = require('../../lib/label').default
const {BaseSprite} = require('sprite-core')

async function drawSprites(canvas, sprites) {
function drawSprites(canvas, sprites) {
const {width, height} = canvas,
context = canvas.getContext('2d')

context.clearRect(0, 0, width, height)
for(let i = 0; i < sprites.length; i++) {
/* eslint-disable no-await-in-loop */
await sprites[i].draw(context, true)
/* eslint-enabel no-await-in-loop */
sprites[i].connect(context).draw()
}
return canvas.toBuffer()
}

const text1 = new Label('SpriteJS.org 中国')

text1.attr({
anchor: 0.5,
pos: [400, 300],
font: '48px Arial',
color: '#fff',
bgcolor: 'blue',
renderMode: 'stroke',
lineHeight: 75,
padding: [0, 50, 0, 50],
const s = new BaseSprite()

s.attr({
bgcolor: 'red',
anchor: [0.5, 0.5],
pos: [100, 100],
size: [50, 50],
})

const canvas = createCanvas(800, 600)
const canvas = createCanvas(200, 200)

;(async function () {
const buffer = await drawSprites(canvas, [text1])
fs.writeFileSync('../../test/img/label-48px-Arial.png', buffer)
const buffer = await drawSprites(canvas, [s])
fs.writeFileSync('../../test/img/basesprite-bgcolor-red.png', buffer)
}())

6 changes: 3 additions & 3 deletions test/basesprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('test bgcolor red', async (t) => {

s.connect(context).draw()

const isEqual = await compare(canvas, 'basesprite-bgcolor-red.png')
const isEqual = await compare(canvas, 'basesprite-bgcolor-red')

t.truthy(isEqual)
})
Expand All @@ -46,7 +46,7 @@ test('test bgcolor green opacity=0.5', async (t) => {

s.connect(context).draw()

const isEqual = await compare(canvas, 'basesprite-bgcolor-green-opacity.png')
const isEqual = await compare(canvas, 'basesprite-bgcolor-green-opacity')

t.truthy(isEqual)
})
Expand Down Expand Up @@ -74,6 +74,6 @@ test('test basesprite-s1-s2-rotate', async (t) => {
const canvas = createCanvas(200, 200)
drawSprites(canvas, [s1, s2])

const isEqual = await compare(canvas, 'basesprite-s1-s2-rotate.png')
const isEqual = await compare(canvas, 'basesprite-s1-s2-rotate')
t.truthy(isEqual)
})
10 changes: 6 additions & 4 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export async function createCanvasFromFile(src) {
return canvas
}

export async function compare(canvas, imgFile) {
export async function compare(canvas, caseId) {
const srcData = canvas.toBuffer()
const desCanvas = await createCanvasFromFile(`./test/img/${imgFile}`)
const desCanvas = await createCanvasFromFile(`./test/img/${caseId}.png`)

const desData = desCanvas.toBuffer()
const N = 32
Expand All @@ -26,9 +26,10 @@ export async function compare(canvas, imgFile) {
const results = await Promise.all([hash1, hash2])

const dist = hamming(...results)
console.warn(colors.cyan(`Hamming distance between canvas and ${imgFile} should be: ${dist}`))
console.warn(colors.cyan(`Hamming distance between canvas and ${caseId} is: ${dist}`))

const diffFile = `./test/img-diff/${imgFile}`
const diffFile = `./test/img-diff/${caseId}.diff.png`
const srcFile = `./test/img-diff/${caseId}.src.png`

const width = canvas.width,
height = canvas.height
Expand Down Expand Up @@ -69,6 +70,7 @@ export async function compare(canvas, imgFile) {

if(!isEqual) {
fs.writeFileSync(diffFile, diffCanvas.toBuffer())
fs.writeFileSync(srcFile, canvas.toBuffer())
} else if(fs.existsSync(diffFile)) {
fs.unlinkSync(diffFile)
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/img-diff/basesprite-bgcolor-red.src.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/img-diff/basesprite-s1-s2-rotate.src.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/img/basesprite-bgcolor-green-opacity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/img/basesprite-bgcolor-red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/img/basesprite-s1-s2-rotate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/img/label-48px-Arial.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5cabfda

Please sign in to comment.