Skip to content

Commit

Permalink
Import roblox-jest and use new expect in array tests (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
RoFlection Bot committed Feb 25, 2021
1 parent f8531e9 commit 974f1ea
Show file tree
Hide file tree
Showing 23 changed files with 328 additions and 259 deletions.
2 changes: 1 addition & 1 deletion Packages/.robloxrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"lint": {
"*": "disabled"
}
}
}
2 changes: 2 additions & 0 deletions bin/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set -ex

echo "Build project"
rojo build test-model.project.json --output model.rbxmx
echo "Remove .robloxrc from jest-roblox"
rm -f Packages/_Index/roblox_jest-roblox/jest-roblox/.robloxrc
echo "Run static analysis"
roblox-cli analyze test-model.project.json
echo "Run tests in DEV"
Expand Down
11 changes: 6 additions & 5 deletions bin/spec.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
local ProcessService = game:GetService("ProcessService")
local Root = script.Parent.LuauPolyfillTestModel

-- Load RoactNavigation source into Packages folder so it's next to Roact as expected
local TestEZ = require(Root.Packages.Dev.TestEZ)
local Packages = Root.Packages
-- Load JestRoblox source into Packages folder so it's next to Roact as expected
local JestRoblox = require(Root.Packages.Dev.JestRoblox)

-- Run all tests, collect results, and report to stdout.
local result = TestEZ.TestBootstrap:run(
{ Root.LuauPolyfill },
TestEZ.Reporters.TextReporter
local result = JestRoblox.TestBootstrap:run(
{ Packages.LuauPolyfill },
JestRoblox.Reporters.TextReporter
)

if result.failureCount == 0 then
Expand Down
4 changes: 1 addition & 3 deletions rotriever.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ content_root = "src"
default_alias = "LuauPolyfill"

[dev_dependencies]
# TODO: Flip over to the jest-roblox repo whenever a release is ready, or if
# there's a particular git branch we'd like to use
TestEZ = "github.com/roblox/[email protected]"
JestRoblox = "github.com/roblox/[email protected]"
52 changes: 26 additions & 26 deletions src/Array/__tests__/concat.spec.lua
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
-- Some tests are adapted from examples at:
-- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
return function()
local concat = require(script.Parent.Parent.concat)
local Array = script.Parent.Parent
local LuauPolyfill = Array.Parent
local concat = require(Array.concat)

local Packages = LuauPolyfill.Parent
local JestRoblox = require(Packages.Dev.JestRoblox)
local jestExpect = JestRoblox.Globals.expect

it("concatenate arrays with single values", function()
local expect: any = expect
expect(concat({ 1 })).toEqual({ 1 })
expect(concat({ 1 }, { 2 })).toEqual({ 1, 2 })
expect(concat({ 1 }, { 2 }, { 3 })).toEqual({ 1, 2, 3 })
jestExpect(concat({ 1 })).toEqual({ 1 })
jestExpect(concat({ 1 }, { 2 })).toEqual({ 1, 2 })
jestExpect(concat({ 1 }, { 2 }, { 3 })).toEqual({ 1, 2, 3 })
end)

it("concatenate arrays with multiple values", function()
local expect: any = expect
expect(concat({ 1 }, { 2, 3 })).toEqual({ 1, 2, 3 })
expect(concat({ 1, 2 }, { 3 })).toEqual({ 1, 2, 3 })
expect(concat({ 1, 2 }, { 3, 4 })).toEqual({ 1, 2, 3, 4 })
expect(concat({ 1, 2 }, { 3, 4 }, { 5, 6 })).toEqual({ 1, 2, 3, 4, 5, 6 })
jestExpect(concat({ 1 }, { 2, 3 })).toEqual({ 1, 2, 3 })
jestExpect(concat({ 1, 2 }, { 3 })).toEqual({ 1, 2, 3 })
jestExpect(concat({ 1, 2 }, { 3, 4 })).toEqual({ 1, 2, 3, 4 })
jestExpect(concat({ 1, 2 }, { 3, 4 }, { 5, 6 })).toEqual({ 1, 2, 3, 4, 5, 6 })
end)

it("concatenate values", function()
local expect: any = expect
expect(concat(1)).toEqual({ 1 })
expect(concat(1, 2)).toEqual({ 1, 2 })
expect(concat(1, 2, 3)).toEqual({ 1, 2, 3 })
expect(concat(1, 2, 3, 4)).toEqual({ 1, 2, 3, 4 })
jestExpect(concat(1)).toEqual({ 1 })
jestExpect(concat(1, 2)).toEqual({ 1, 2 })
jestExpect(concat(1, 2, 3)).toEqual({ 1, 2, 3 })
jestExpect(concat(1, 2, 3, 4)).toEqual({ 1, 2, 3, 4 })
end)

it("concatenate values and arrays combination", function()
local expect: any = expect
expect(concat(1, { 2 })).toEqual({ 1, 2 })
expect(concat({ 1 }, 2)).toEqual({ 1, 2 })
expect(concat({ 1 }, 2, { 3 })).toEqual({ 1, 2, 3 })
expect(concat({ 1, 2 }, 3, { 4 })).toEqual({ 1, 2, 3, 4 })
jestExpect(concat(1, { 2 })).toEqual({ 1, 2 })
jestExpect(concat({ 1 }, 2)).toEqual({ 1, 2 })
jestExpect(concat({ 1 }, 2, { 3 })).toEqual({ 1, 2, 3 })
jestExpect(concat({ 1, 2 }, 3, { 4 })).toEqual({ 1, 2, 3, 4 })
end)

it("concatenates values to an array", function()
local expect: any = expect
local letters = { "a", "b", "c" }
local alphaNumeric = concat(letters, 1, { 2, 3 })
expect(alphaNumeric).toEqual({ "a", "b", "c", 1, 2, 3 })
jestExpect(alphaNumeric).toEqual({ "a", "b", "c", 1, 2, 3 })
end)

it("concatenates nested arrays", function()
local expect: any = expect
local num1 = { { 1 } }
local num2 = { 2, { 3 } }
local numbers = concat(num1, num2)
expect(numbers).toEqual({ { 1 }, 2, { 3 } })
jestExpect(numbers).toEqual({ { 1 }, 2, { 3 } })
end)

if _G.__DEV__ then
it("throws when an object-like table value is passed", function()
expect(function()
jestExpect(function()
concat({1, 2}, { a = true })
end).to.throw("Array.concat(...) only works with array-like tables but it received an object-like table")
end).toThrow("Array.concat(...) only works with array-like tables but it received an object-like table")
end)
end
end
50 changes: 28 additions & 22 deletions src/Array/__tests__/every.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,76 @@
-- Tests adapted directly from examples at:
-- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
return function()
local every = require(script.Parent.Parent.every)
local Array = script.Parent.Parent
local LuauPolyfill = Array.Parent
local every = require(Array.every)

local Packages = LuauPolyfill.Parent
local JestRoblox = require(Packages.Dev.JestRoblox)
local jestExpect = JestRoblox.Globals.expect

it("Invalid argument", function()
expect(function()
jestExpect(function()
every(nil, function() end)
end).to.throw()
expect(function()
end).toThrow()
jestExpect(function()
every({0, 1}, nil)
end).to.throw()
end).toThrow()
end)

it("Testing size of all array elements", function()
local isBigEnough = function(element, index, array)
local isBigEnough = function(element, index, array)
return element >= 10
end
expect(every(
jestExpect(every(
{12, 5, 8, 130, 44},
isBigEnough
)).to.equal(false)
expect(every(
)).toEqual(false)
jestExpect(every(
{12, 54, 18, 130, 44},
isBigEnough
)).to.equal(true)
)).toEqual(true)
end)

it("Modifying initial array", function()
local arr = {1, 2, 3, 4}
local expected = {1, 1, 2}
expect(every(
jestExpect(every(
arr,
function(elem, index, a)
a[index + 1] -= 1
expect(a[index]).to.equal(expected[index])
jestExpect(a[index]).toEqual(expected[index])
return elem < 2
end
)).to.equal(false)
expect(arr).toEqual({1, 1, 2, 3})
)).toEqual(false)
jestExpect(arr).toEqual({1, 1, 2, 3})
end)

it("Appending to initial array", function()
local arr = {1, 2, 3}
local expected = {1, 2, 3}
expect(every(
jestExpect(every(
arr,
function(elem, index, a)
table.insert(a, "new")
expect(a[index]).to.equal(expected[index])
jestExpect(a[index]).toEqual(expected[index])
return elem < 4
end
)).to.equal(true)
expect(arr).toEqual({1, 2, 3, "new", "new", "new"})
)).toEqual(true)
jestExpect(arr).toEqual({1, 2, 3, "new", "new", "new"})
end)

it("Deleting from inital array", function()
local arr = {1, 2, 3, 4}
local expected = {1, 2}
expect(every(
jestExpect(every(
arr,
function(elem, index, a)
table.remove(a)
expect(a[index]).to.equal(expected[index])
jestExpect(a[index]).toEqual(expected[index])
return elem < 4
end
)).to.equal(true)
expect(arr).toEqual({1, 2})
)).toEqual(true)
jestExpect(arr).toEqual({1, 2})
end)
end
30 changes: 17 additions & 13 deletions src/Array/__tests__/filter.spec.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
-- FIXME: roblox-cli has special, hard-coded types for TestEZ that break when we
-- use custom matchers added via `expect.extend`
--!nocheck

-- Tests adapted directly from examples at:
-- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

return function()
local filter = require(script.Parent.Parent.filter)
local isFinite = require(script.Parent.Parent.Parent.Number.isFinite)
local Array = script.Parent.Parent
local LuauPolyfill = Array.Parent
local filter = require(Array.filter)
local isFinite = require(LuauPolyfill.Number.isFinite)

local Packages = LuauPolyfill.Parent
local JestRoblox = require(Packages.Dev.JestRoblox)
local jestExpect = JestRoblox.Globals.expect

it("Filtering out all small values", function()
local isBigEnough = function(value)
return value >= 10
end

local filtered = filter({12, 5, 8, 130, 44}, isBigEnough)
expect(filtered).toEqual({12, 130, 44})
jestExpect(filtered).toEqual({12, 130, 44})
end)

it("Find all prime numbers in an array", function()
Expand All @@ -34,7 +38,7 @@ return function()
{-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
isPrime
)
expect(filtered).toEqual({2, 3, 5, 7, 11, 13})
jestExpect(filtered).toEqual({2, 3, 5, 7, 11, 13})
end)

it("Filtering invalid entries from JSON", function()
Expand All @@ -61,10 +65,10 @@ return function()
end

local arrByID = filter(arr, filterByID)
expect(arrByID).toEqual(
jestExpect(arrByID).toEqual(
{{ id = 15 }, { id = -1 }, { id = 3 }, { id = 12.2 }}
)
expect(invalidEntries).to.equal(5)
jestExpect(invalidEntries).toEqual(5)
end)

it("Searching in array", function()
Expand All @@ -75,8 +79,8 @@ return function()
end)
end

expect(filterItems(fruits, "ap")).toEqual({"apple", "grapes"})
expect(filterItems(fruits, "an")).toEqual({"banana", "mango", "orange"})
jestExpect(filterItems(fruits, "ap")).toEqual({"apple", "grapes"})
jestExpect(filterItems(fruits, "an")).toEqual({"banana", "mango", "orange"})
end)

describe("Affecting Initial Array", function()
Expand All @@ -94,7 +98,7 @@ return function()
end
)

expect(modifiedWords).toEqual({"spray"})
jestExpect(modifiedWords).toEqual({"spray"})
end)

it("Appending to initial array", function()
Expand All @@ -108,7 +112,7 @@ return function()
end
)

expect(modifiedWords).toEqual({"spray", "limit", "elite"})
jestExpect(modifiedWords).toEqual({"spray", "limit", "elite"})
end)

it("Deleting from initial array", function()
Expand All @@ -122,7 +126,7 @@ return function()
end
)

expect(modifiedWords).toEqual({"spray", "limit"})
jestExpect(modifiedWords).toEqual({"spray", "limit"})
end)
end)
end
16 changes: 9 additions & 7 deletions src/Array/__tests__/find.spec.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
return function()
local Array = script.Parent.Parent
local LuauPolyfill = Array.Parent
local find = require(Array.find)

local Packages = LuauPolyfill.Parent
local JestRoblox = require(Packages.Dev.JestRoblox)
local jestExpect = JestRoblox.Globals.expect

local function returnTrue()
return true
end
Expand All @@ -11,18 +16,18 @@ return function()
end

it("returns nil if the array is empty", function()
expect(find({}, returnTrue)).to.equal(nil)
jestExpect(find({}, returnTrue)).toEqual(nil)
end)

it("returns nil if the predicate is always false", function()
expect(find({1, 2, 3}, returnFalse)).to.equal(nil)
jestExpect(find({1, 2, 3}, returnFalse)).toEqual(nil)
end)

it("returns the first element where the predicate is true", function()
local result = find({3, 4, 5, 6}, function(element)
return element % 2 == 0
end)
expect(result).to.equal(4)
jestExpect(result).toEqual(4)
end)

it("passes the element, its index and the array to the predicate", function()
Expand All @@ -31,9 +36,6 @@ return function()
find(array, function(...)
arguments = {...}
end)
expect(#arguments).to.equal(3)
expect(arguments[1]).to.equal("foo")
expect(arguments[2]).to.equal(1)
expect(arguments[3]).to.equal(array)
jestExpect(arguments).toEqual({"foo", 1, array})
end)
end
Loading

0 comments on commit 974f1ea

Please sign in to comment.