1
mirror of https://gitlab.com/jessieh/qrprinter.git synced 2024-12-22 01:41:46 +00:00

Add testing

This commit is contained in:
Jessie Hildebrandt 2021-12-11 20:42:02 -05:00
parent 3a83ed14e0
commit 736c0861c8
14 changed files with 446 additions and 1 deletions

5
.busted Normal file
View File

@ -0,0 +1,5 @@
return {
default = {
coverage = true
}
}

3
.gitignore vendored
View File

@ -10,3 +10,6 @@ build/
# Luarocks source tree files
*.src.rock
# luacov report files
luacov.*

11
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,11 @@
image: jessiehildebrandt/luadev:luajit
test:
script:
- luarocks build
- luarocks test
- luacov
- cat luacov.report.out
artifacts:
paths:
- luacov.report.out

4
.luacov Normal file
View File

@ -0,0 +1,4 @@
modules = {
["qrprinter"] = "build/qrprinter.lua",
["qrprinter.ffi.qrencode"] = "build/ffi/qrencode.lua",
}

View File

@ -2,8 +2,12 @@
A wrapper for `libqrencode` that provides an easy way of encoding and printing strings as QR codes from Lua.
**This uses LuaJIT's FFI library, and so will not run in PUC Lua implementations.**
Written in [Fennel](https://fennel-lang.org).
[![Test Status](https://gitlab.com/jessieh/qrprinter/badges/main/pipeline.svg)](https://gitlab.com/jessieh/qrprinter/-/commits/main) [![Code Coverage](https://gitlab.com/jessieh/qrprinter/badges/main/coverage.svg)](https://gitlab.com/jessieh/qrprinter/-/commits/main)
## Installation
Install dependencies:
@ -69,3 +73,15 @@ Print `qr` to `stdout` using ANSI escape sequences.
qrprinter.print_qr_utf8( qr, [options] )
```
Print `qr` to `stdout` using UTF8 block elements.
## Testing
The Fennel code needs to be compiled before tests can be run:
```bash
# Build the project:
luarocks build
# Now you can run tests:
luarocks test
```

View File

@ -0,0 +1,45 @@
package = 'qrprinter'
version = '1.1-0'
rockspec_format = '3.0'
supported_platforms = {'linux'}
source = {
url = "git+https://gitlab.com/jessieh/qrprinter",
tag = "1.1-0"
}
description = {
summary = "Encode and print strings as QR codes.",
detailed = "qrprinter wraps the libqrencode C library using LuaJIT's FFI to provide an easy way of encoding and printing strings as QR codes from Lua.",
homepage = "https://gitlab.com/jessieh/qrprinter",
maintainer = "Jessie Hildebrandt <jessieh@jessieh.net>",
license = "GPLv3"
}
dependencies = {
"lua >= 5.1",
"luajit >= 2.0"
}
external_dependencies = {
LIBQRENCODE = {
header = "qrencode.h"
}
}
test = {
type = 'busted'
}
test_dependencies = {
'busted = 2.0.0-1',
'luacov = 0.15.0-1'
}
build = {
type = "make",
makefile = "Makefile",
build_variables = {
-- This suppresses luarocks' CFLAGS warning-- we don't actually use this
CFLAGS = "$(CFLAGS)"
},
install = {
lua = {
["qrprinter"] = "build/qrprinter.lua",
["qrprinter.ffi.qrencode"] = "build/ffi/qrencode.lua"
}
}
}

View File

@ -0,0 +1,97 @@
-- spec/encode_string_spec.lua
-- Specifies tests for qrprinter.encode_string functionality
--------------------------------------------------------------------------------
-- Disable LuaJIT compiler
-- LuaJIT's optimizations can frob luacov's coverage reports
require( 'jit' ).off()
--------------------------------------------------------------------------------
-- Module imports
qrprinter = require( 'qrprinter' )
--------------------------------------------------------------------------------
-- Test context
describe( 'qrprinter.encode_string functionality', function ()
----------------------------------------
-- Test definitions
it( 'returns nil for empty strings', function()
local expected_value = nil
local empty_string = ''
assert.are.same( expected_value, qrprinter.encode_string( empty_string ) )
end )
it( 'encodes alphanumeric strings', function()
local expected_value = {
{ true, true, true, true, true, true, true, false, false, true, false, false, true, false, true, true, true, true, true, true, true },
{ true, false, false, false, false, false, true, false, true, false, false, true, false, false, true, false, false, false, false, false, true },
{ true, false, true, true, true, false, true, false, false, true, false, false, false, false, true, false, true, true, true, false, true },
{ true, false, true, true, true, false, true, false, true, false, false, true, false, false, true, false, true, true, true, false, true },
{ true, false, true, true, true, false, true, false, false, false, true, true, true, false, true, false, true, true, true, false, true },
{ true, false, false, false, false, false, true, false, true, true, true, false, true, false, true, false, false, false, false, false, true },
{ true, true, true, true, true, true, true, false, true, false, true, false, true, false, true, true, true, true, true, true, true },
{ false, false, false, false, false, false, false, false, false, false, true, true, true, false, false, false, false, false, false, false, false },
{ true, true, true, true, true, false, true, true, true, true, false, false, true, true, false, true, false, true, false, true, false },
{ false, false, true, true, false, false, false, false, false, true, false, false, true, false, false, true, false, false, true, false, true },
{ true, false, false, false, false, true, true, false, false, false, true, true, false, true, false, false, true, false, false, true, false },
{ true, true, true, true, true, false, false, false, false, true, false, false, false, false, false, true, true, true, true, true, false },
{ true, false, false, false, true, true, true, true, false, false, true, true, false, true, false, false, true, false, false, false, false },
{ false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, false, false, true, false, true },
{ true, true, true, true, true, true, true, false, true, true, false, false, true, false, true, true, false, true, false, true, false },
{ true, false, false, false, false, false, true, false, false, false, true, true, true, true, true, false, true, false, true, false, true },
{ true, false, true, true, true, false, true, false, true, false, true, false, true, false, false, true, false, true, false, true, false },
{ true, false, true, true, true, false, true, false, true, true, false, false, true, false, false, false, true, false, true, false, false },
{ true, false, true, true, true, false, true, false, true, false, true, true, false, true, false, true, false, true, true, false, false },
{ true, false, false, false, false, false, true, false, true, false, true, false, false, false, false, true, true, false, true, false, false },
{ true, true, true, true, true, true, true, false, true, true, true, true, false, true, false, true, false, true, false, true, false }
}
local test_string = 'abc123'
assert.are.same( expected_value, qrprinter.encode_string( test_string ) )
end )
it( 'encodes UTF-8 strings', function()
local expected_value = {
{ true, true, true, true, true, true, true, false, false, false, true, false, true, false, true, true, true, true, true, true, true },
{ true, false, false, false, false, false, true, false, false, false, false, false, true, false, true, false, false, false, false, false, true },
{ true, false, true, true, true, false, true, false, true, false, true, false, false, false, true, false, true, true, true, false, true },
{ true, false, true, true, true, false, true, false, false, false, false, false, true, false, true, false, true, true, true, false, true },
{ true, false, true, true, true, false, true, false, false, true, false, true, true, false, true, false, true, true, true, false, true },
{ true, false, false, false, false, false, true, false, false, true, true, true, false, false, true, false, false, false, false, false, true },
{ true, true, true, true, true, true, true, false, true, false, true, false, true, false, true, true, true, true, true, true, true },
{ false, false, false, false, false, false, false, false, true, false, true, false, false, false, false, false, false, false, false, false, false },
{ true, true, true, false, true, true, true, true, true, false, true, false, true, true, true, false, false, false, true, false, false },
{ false, false, true, false, false, true, false, true, false, true, false, true, false, false, false, false, true, false, true, false, false },
{ false, true, true, false, true, false, true, false, true, true, false, true, false, true, true, false, false, true, false, false, false },
{ false, false, true, false, false, false, false, true, true, true, true, true, true, false, true, true, false, true, true, true, false },
{ true, true, true, false, true, true, true, false, false, false, false, true, false, true, false, true, true, true, true, false, true },
{ false, false, false, false, false, false, false, false, true, true, false, true, false, true, false, false, false, true, false, false, true },
{ true, true, true, true, true, true, true, false, true, true, true, false, true, false, true, false, false, false, false, true, false },
{ true, false, false, false, false, false, true, false, true, true, false, false, true, true, true, false, true, true, true, true, false },
{ true, false, true, true, true, false, true, false, true, false, false, true, true, true, true, true, false, true, true, true, false },
{ true, false, true, true, true, false, true, false, false, true, false, false, true, false, true, false, false, true, true, true, false },
{ true, false, true, true, true, false, true, false, true, false, true, false, false, true, false, false, false, true, true, false, true },
{ true, false, false, false, false, false, true, false, true, true, false, true, false, true, true, true, false, false, true, true, false },
{ true, true, true, true, true, true, true, false, true, true, true, true, true, true, true, false, true, true, false, true, true }
}
local test_string = '💙🔵🔷'
assert.are.same( expected_value, qrprinter.encode_string( test_string ) )
end )
end )

View File

@ -0,0 +1,25 @@
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         

View File

@ -0,0 +1,25 @@
##################################################
##################################################
#### #### #### ## ####
#### ########## ## #### #### ########## ####
#### ## ## #### ######## ## ## ####
#### ## ## ## #### #### ## ## ####
#### ## ## ###### ## ## ## ####
#### ########## ## ## ## ########## ####
#### ## ## ## ## ####
######################## ####################
#### ## #### ## ## ## ######
######## ########## #### #### #### ## ####
#### ######## ###### ## #### #### ######
#### ######## ########## ######
#### ###### #### ## #### ############
#################### #### ## ####
#### ## #### ## ## ## ######
#### ########## ###### ## ## ## ####
#### ## ## ## ## ## #### ## ## ######
#### ## ## ## #### ###### ## ########
#### ## ## ## ## ## ## ## ########
#### ########## ## ## ######## ## ########
#### ## ## ## ## ## ######
##################################################
##################################################

View File

@ -0,0 +1,25 @@
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         
                         

View File

@ -0,0 +1,21 @@
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     
                     

View File

@ -0,0 +1,27 @@
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           
                           

View File

@ -0,0 +1,25 @@
██████████████████████████████████████████████████
██████████████████████████████████████████████████
████ ████ ████ ██ ████
████ ██████████ ██ ████ ████ ██████████ ████
████ ██ ██ ████ ████████ ██ ██ ████
████ ██ ██ ██ ████ ████ ██ ██ ████
████ ██ ██ ██████ ██ ██ ██ ████
████ ██████████ ██ ██ ██ ██████████ ████
████ ██ ██ ██ ██ ████
████████████████████████ ████████████████████
████ ██ ████ ██ ██ ██ ██████
████████ ██████████ ████ ████ ████ ██ ████
████ ████████ ██████ ██ ████ ████ ██████
████ ████████ ██████████ ██████
████ ██████ ████ ██ ████ ████████████
████████████████████ ████ ██ ████
████ ██ ████ ██ ██ ██ ██████
████ ██████████ ██████ ██ ██ ██ ████
████ ██ ██ ██ ██ ██ ████ ██ ██ ██████
████ ██ ██ ██ ████ ██████ ██ ████████
████ ██ ██ ██ ██ ██ ██ ██ ████████
████ ██████████ ██ ██ ████████ ██ ████████
████ ██ ██ ██ ██ ██ ██████
██████████████████████████████████████████████████
██████████████████████████████████████████████████

116
spec/print_qr_spec.lua Normal file
View File

@ -0,0 +1,116 @@
-- spec/encode_string_spec.lua
-- Specifies tests for qrprinter.encode_string functionality
--------------------------------------------------------------------------------
-- Disable LuaJIT compiler
-- LuaJIT's optimizations can frob luacov's coverage reports
require( 'jit' ).off()
--------------------------------------------------------------------------------
-- Module imports
qrprinter = require( 'qrprinter' )
--------------------------------------------------------------------------------
-- Helper functions
local function read_file_into_string( path )
-- Open file at `path`
local file = io.open( path, 'r' )
if not file then
error( 'Error opening file at ' .. path .. ' during test.' )
end
-- Read file contents
local content = file:read( '*a' )
-- Remove terminating newline
local last_char = string.sub( content, -1 )
if last_char == '\n' then
content = string.sub( content, 1, -2 )
end
file:close()
return content
end
--------------------------------------------------------------------------------
-- Test context
describe( 'qrprinter.print_qr functionality', function ()
----------------------------------------
-- Test-wide values
local test_qr = qrprinter.encode_string( 'abc123' )
----------------------------------------
-- Replace print with mock function that returns the provided string
setup( function()
_G.print = function( msg )
return msg
end
end )
----------------------------------------
-- Test definitions
it( 'aliases print_qr to print_qr_ansi', function()
assert.are.same( qrprinter.print_qr( test_qr ), qrprinter.print_qr_ansi( test_qr ) )
end )
it( 'prints QR codes using ASCII characters', function()
local expected_output = read_file_into_string( 'spec/expected_output/print_qr_ascii.txt' )
assert.are.same( expected_output, qrprinter.print_qr_ascii( test_qr ) )
end )
it( 'prints QR codes using ANSI escape sequences', function()
local expected_output = read_file_into_string( 'spec/expected_output/print_qr_ansi.txt' )
assert.are.same( expected_output, qrprinter.print_qr_ansi( test_qr ) )
end )
it( 'prints QR codes using UTF8 block sequences', function()
local expected_output = read_file_into_string( 'spec/expected_output/print_qr_utf8.txt' )
assert.are.same( expected_output, qrprinter.print_qr_utf8( test_qr ) )
end )
it( 'adjusts padding of the output when the "padding" option is provided', function()
local padding_0_expected_output = read_file_into_string( 'spec/expected_output/print_qr_padding_0.txt' )
local padding_3_expected_output = read_file_into_string( 'spec/expected_output/print_qr_padding_3.txt' )
assert.are.same( padding_0_expected_output, qrprinter.print_qr( test_qr, { padding = 0 } ) )
assert.are.same( padding_3_expected_output, qrprinter.print_qr( test_qr, { padding = 3 } ) )
end )
it( 'inverts the colors of the output when the "invert" option is set', function()
local expected_output = read_file_into_string( 'spec/expected_output/print_qr_invert_true.txt' )
assert.are.same( expected_output, qrprinter.print_qr( test_qr, { invert = true } ) )
end )
end )