Skip to content

Commit

Permalink
Tested on Lua 5.1 to 5.4.
Browse files Browse the repository at this point in the history
  • Loading branch information
oo13 committed Jun 1, 2024
1 parent b9d6579 commit 773e148
Show file tree
Hide file tree
Showing 15 changed files with 1,232 additions and 965 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: unit_test
on:
push:
paths:
- '*.lua'
- 'test/*'
pull_request:
paths:
- '*.lua'
- 'test/*'

jobs:
unit_test:
permissions:
contents: read
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix:
version: [5.1, 5.2, 5.3, 5.4]
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Lua
run: sudo apt-get install lua${{ matrix.version }} liblua${{ matrix.version }}-dev luarocks

- name: Install lua-utf8
run: luarocks --local install luautf8

- name: Run Test
working-directory: test
run: ./test_all ${{ matrix.version }}
6 changes: 2 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Translatable phrase generator for Lua 5.1, or Endless names plugin for Naev
# Translatable phrase generator for Lua, or Endless names plugin for Naev

This Lua module is one of the translatable phrase generators. See [the manual](manual.md) for details. The module consists of ["scripts/phrase.lua"](scripts/phrase.lua), ["scripts/phrase/data.lua"](scripts/phrase/data.lua), and ["scripts/phrase/parser.lua"](scripts/phrase/parser.lua). Endless names plugin for Naev is a simple example of use. The user codes are ["scripts/pilotname/*.lua"](scripts/pilotname/), and the Japanese translation is ["po/ja.po"](po/ja.po).

Expand Down Expand Up @@ -79,9 +79,7 @@ main = あなたは{ECONOMICAL_SITUATION}です。 |

# Requirement

This module supports Lua 5.1, and requires [lua-utf8](https://github.com/starwing/luautf8) unless you stick to the plain 8-bit character encoding.

This module has not tested in Lua 5.2 and higher.
This module supports Lua 5.1 to 5.4, and requires [lua-utf8](https://github.com/starwing/luautf8) unless you stick to the plain 8-bit character encoding.

# License
This module is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Expand Down
4 changes: 1 addition & 3 deletions manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,7 @@ Note:

# Requirement

This module supports Lua 5.1, and requires [lua-utf8](https://github.com/starwing/luautf8) unless you stick to the plain 8-bit character encoding.

This module has not tested in Lua 5.2 and higher.
This module supports Lua 5.1 to 5.4, and requires [lua-utf8](https://github.com/starwing/luautf8) unless you stick to the plain 8-bit character encoding.

# License
This module is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Expand Down
2 changes: 1 addition & 1 deletion scripts/phrase/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Copyright © 2024 OOTA, Masato

local utf8_support, utf8 = pcall(require, "utf8") -- Naev provides lua-utf8 as "utf8".
if not utf8_support or not utf8.gsub then
utf8_support, utf8 = pcall(require, "lua-utf8") -- https://github.com/starwing/luautf8
utf8_support, utf8 = pcall(require, "lua-utf8") -- luarocks --local install luautf8, or https://github.com/starwing/luautf8
end

-- 8-bit encoding version.
Expand Down
63 changes: 63 additions & 0 deletions test/test_8bit_encoding.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
--[[
Test 8 bit encoding
The test succeeds only in 8 bit encoding.
-]]
local unit_test = require("unit_test")
local phrase = require("phrase")

local test_8bit_encoding = {}

function test_8bit_encoding.run_test()
local ut = unit_test.new()

-- select the first option.
phrase.set_random_function(ut:get_stub_random())

-- override the function to check if some errors are detected.
local error_messages = ""
local error_is_expected = false -- It can be changed in the test functions.
function phrase.output_error(err_msg)
error_messages = error_messages .. err_msg
if not error_is_expected then
io.stderr:write(err_msg)
end
end

-- Clear the error messages.
ut:set_enter(function ()
error_messages = ""
error_is_expected = false
end)
-- Check if some errors are detected.
ut:set_leave(function ()
return error_is_expected ~= (error_messages == "")
end)


local tests = {}

function tests.parse_error_message()
local ph = phrase.new()
error_is_expected = true
ph:add([[
main {* ああ } = text1 | text2 | text3 ~ /pat1/repl1/ ~ /pat2/repl2/g
]])
return error_messages == [[
Error in the phrase "main {* ああ } = text1 | text2 | text3 ...":
Line#1, Column#18: "=" or ":=" is expected.
]]
end

function tests.gsub_substitution()
local ph = phrase.new()
ph:add([[
main = あいう ~ /[あ]/か/]])
local r = ph:generate()
return r == "\129\130いう"
end

return ut:runner("8 Bit Encoding Test", tests, { verbose = false })
end

return test_8bit_encoding
9 changes: 9 additions & 0 deletions test/test_all
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
set -e

VER=$1

luarocks config lua_version ${VER}
eval `luarocks path`
lua${VER} test_main_normal.lua
lua${VER} test_encoding_error.lua
13 changes: 0 additions & 13 deletions test/test_all.lua

This file was deleted.

96 changes: 96 additions & 0 deletions test/test_encoding_error.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
--[[
Test to raise the encoding error
The test under the condition that fails to require("lua-utf8").
--]]
local unit_test = require("unit_test")

-- must fail to require("lua-utf8")
package.path = "./?.lua;../scripts/?.lua"
package.cpath = ""
local phrase = require("phrase")


local test_encoding_error = {}

function test_encoding_error.run_test()
local ut = unit_test.new()

-- select the first option.
phrase.set_random_function(ut:get_stub_random())

-- override the function to check if some errors are detected.
local error_messages = ""
local error_is_expected = false -- It can be changed in the test functions.
function phrase.output_error(err_msg)
error_messages = error_messages .. err_msg
if not error_is_expected then
io.stderr:write(err_msg)
end
end

-- Clear the error messages and the random sequence.
ut:set_enter(function ()
phrase.require_utf8(false)
ut:set_random_sequence({})
error_messages = ""
error_is_expected = false
end)
-- Check if some errors are detected.
ut:set_leave(function ()
return error_is_expected ~= (error_messages == "")
end)


local tests = {}

function tests.new_fatal_error_without_parameter()
phrase.require_utf8(true)
error_is_expected = true
local ph = phrase.new()
return ph == nil
end

function tests.new_fatal_error_with_a_parameter()
phrase.require_utf8(true)
error_is_expected = true
local ph = phrase.new([[main = A]])
return ph == nil
end

function tests.compile_fatal_error_without_parameter()
phrase.require_utf8(true)
error_is_expected = true
local compiled = phrase.compile()
return compiled == nil
end

function tests.compile_fatal_error_with_a_parameter()
phrase.require_utf8(true)
error_is_expected = true
local compiled = phrase.compile([[main = A]])
return compiled == nil
end

function tests.compile_add_fatal_error()
local compiled = phrase.compile([[A = 1]])
phrase.require_utf8(true)
error_is_expected = true
local result = compiled:add([[B = 2]])
return
not result and
compiled.type_compiled_syntax and
compiled.data.type_syntax and
compiled.data.assignments["A"] and
not compiled.data.assignments["B"]
end

return ut:runner("Function Encoding Error", tests, { verbose = false })
end

if test_encoding_error.run_test() > 0 then
os.exit(1)
else
os.exit(0)
end

Loading

0 comments on commit 773e148

Please sign in to comment.