Skip to content

Commit

Permalink
Add some tests for globbing
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeterv committed Jul 17, 2015
1 parent da8dbb9 commit d65cc26
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions spec/globbing_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
local globbing = require "luacheck.globbing"

describe("globbing", function()
describe("match", function()
it("returns true on literal match", function()
assert.is_true(globbing.match("foo/bar", "foo/bar"))
end)

it("returns true on literal match after normalization", function()
assert.is_true(globbing.match("foo//bar/baz/..", "./foo/bar/"))
end)

it("returns false for on literal mismatch", function()
assert.is_false(globbing.match("foo/bar", "foo/baz"))
end)

it("accepts subdirectory matches", function()
assert.is_true(globbing.match("foo/bar", "foo/bar/baz"))
end)

it("understands wildcards", function()
assert.is_true(globbing.match("*", "foo"))
assert.is_true(globbing.match("foo/*r", "foo/bar"))
assert.is_true(globbing.match("foo/*r", "foo/bar/baz"))
assert.is_false(globbing.match("foo/*r", "foo/baz"))
end)

it("understands optional characters", function()
assert.is_false(globbing.match("?", "foo"))
assert.is_true(globbing.match("???", "foo"))
assert.is_true(globbing.match("????", "foo"))
assert.is_true(globbing.match("f?o/?a?", "foo/bar"))
assert.is_false(globbing.match("f?o/?a?", "foo/abc"))
end)

it("understands ranges and classes", function()
assert.is_true(globbing.match("[d-h]o[something]", "foo"))
assert.is_false(globbing.match("[d-h]o[somewhere]", "bar"))
assert.is_false(globbing.match("[.-h]o[i-z]", "bar"))
end)

it("accepts closing bracket as first class character", function()
assert.is_true(globbing.match("[]]", "]"))
assert.is_false(globbing.match("[]]", "["))
assert.is_true(globbing.match("[]foo][]foo][]foo]", "foo"))
end)

it("accepts dash as first or last class character", function()
assert.is_true(globbing.match("[-]", "-"))
assert.is_false(globbing.match("[-]", "+"))
assert.is_true(globbing.match("[---]", "-"))
end)

it("understands negation", function()
assert.is_true(globbing.match("[!foo][!bar][!baz]", "boo"))
assert.is_false(globbing.match("[!foo][!bar][!baz]", "far"))
assert.is_false(globbing.match("[!a-z]", "g"))
end)

it("understands recursive globbing using **", function()
assert.is_true(globbing.match("**/*.lua", "foo.lua"))
assert.is_true(globbing.match("**/*.lua", "foo/bar.lua"))
assert.is_false(globbing.match("foo/**/*.lua", "bar.lua"))
assert.is_false(globbing.match("foo/**/*.lua", "foo.lua"))
assert.is_true(globbing.match("foo/**/bar/*.lua", "foo/bar/baz.lua"))
assert.is_true(globbing.match("foo/**/bar/*.lua", "foo/foo2/foo3/bar/baz.lua"))
assert.is_false(globbing.match("foo/**/bar/*.lua", "foo/baz.lua"))
assert.is_false(globbing.match("foo/**/bar/*.lua", "bar/baz.lua"))
end)
end)
end)

0 comments on commit d65cc26

Please sign in to comment.