Skip to content

Commit 23b24d1

Browse files
authored
Check uniqueness of column selection & Add more tests (#3)
1 parent 5532d5c commit 23b24d1

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

src/selectors.jl

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ struct IndexSelector <: ColumnSelector
55
if isempty(inds)
66
throw(ArgumentError("column selection cannot be empty"))
77
end
8+
if !allunique(inds)
9+
throw(ArgumentError("column indices must be unique"))
10+
end
811
new(inds)
912
end
1013
end
@@ -23,6 +26,9 @@ struct NameSelector <: ColumnSelector
2326
if isempty(names)
2427
throw(ArgumentError("column selection cannot be empty"))
2528
end
29+
if !allunique(names)
30+
throw(ArgumentError("column names must be unique"))
31+
end
2632
new(names)
2733
end
2834
end

test/runtests.jl

+42-12
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,81 @@ using Test
55
vecnames = [:a, :b, :c, :d, :e, :f]
66
tupnames = (:a, :b, :c, :d, :e, :f)
77

8-
# vector of integers
8+
# index selector: vector of integers
99
selector = CS.selector([1, 3, 5])
10+
@test selector isa CS.IndexSelector
1011
snames = selector(vecnames)
1112
@test snames == [:a, :c, :e]
1213
snames = selector(tupnames)
1314
@test snames == [:a, :c, :e]
1415

15-
# tuple of integers
16+
# index selector: tuple of integers
1617
selector = CS.selector((1, 3, 5))
18+
@test selector isa CS.IndexSelector
1719
snames = selector(vecnames)
1820
@test snames == [:a, :c, :e]
1921
snames = selector(tupnames)
2022
@test snames == [:a, :c, :e]
2123

22-
# vector of symbols
24+
# name selector: vector of symbols
2325
selector = CS.selector([:a, :c, :e])
26+
@test selector isa CS.NameSelector
2427
snames = selector(vecnames)
2528
@test snames == [:a, :c, :e]
2629
snames = selector(tupnames)
2730
@test snames == [:a, :c, :e]
2831

29-
# tuple of symbols
32+
# name selector: tuple of symbols
3033
selector = CS.selector((:a, :c, :e))
34+
@test selector isa CS.NameSelector
3135
snames = selector(vecnames)
3236
@test snames == [:a, :c, :e]
3337
snames = selector(tupnames)
3438
@test snames == [:a, :c, :e]
3539

36-
# vector of strings
40+
# name selector: vector of strings
3741
selector = CS.selector(["a", "c", "e"])
42+
@test selector isa CS.NameSelector
3843
snames = selector(vecnames)
3944
@test snames == [:a, :c, :e]
4045
snames = selector(tupnames)
4146
@test snames == [:a, :c, :e]
4247

43-
# tuple of strings
48+
# name selector: tuple of strings
4449
selector = CS.selector(("a", "c", "e"))
50+
@test selector isa CS.NameSelector
4551
snames = selector(vecnames)
4652
@test snames == [:a, :c, :e]
4753
snames = selector(tupnames)
4854
@test snames == [:a, :c, :e]
4955

50-
# regex
56+
# regex selector: regex
5157
selector = CS.selector(r"[ace]")
58+
@test selector isa CS.RegexSelector
5259
snames = selector(vecnames)
5360
@test snames == [:a, :c, :e]
5461
snames = selector(tupnames)
5562
@test snames == [:a, :c, :e]
5663

57-
# colon
64+
# all selector: colon
5865
selector = CS.selector(:)
66+
@test selector isa CS.AllSelector
5967
snames = selector(vecnames)
6068
@test snames == [:a, :b, :c, :d, :e, :f]
6169
snames = selector(tupnames)
6270
@test snames == [:a, :b, :c, :d, :e, :f]
6371

64-
# nothing
72+
# none selector: nothing
6573
selector = CS.selector(nothing)
74+
@test selector isa CS.NoneSelector
6675
snames = selector(vecnames)
6776
@test snames == Symbol[]
6877
snames = selector(tupnames)
6978
@test snames == Symbol[]
7079

71-
# single integer
80+
# single index selector: integer
7281
selector = CS.selector(1)
82+
@test selector isa CS.SingleIndexSelector
7383
sname = CS.selectsingle(selector, vecnames)
7484
snames = CS.select(selector, vecnames)
7585
@test sname == :a
@@ -79,8 +89,9 @@ using Test
7989
@test sname == :a
8090
@test snames == [:a]
8191

82-
# single symbol
92+
# single name selector: symbol
8393
selector = CS.selector(:b)
94+
@test selector isa CS.SingleNameSelector
8495
sname = CS.selectsingle(selector, vecnames)
8596
snames = CS.select(selector, vecnames)
8697
@test sname == :b
@@ -90,8 +101,9 @@ using Test
90101
@test sname == :b
91102
@test snames == [:b]
92103

93-
# single string
104+
# single name selector: string
94105
selector = CS.selector("c")
106+
@test selector isa CS.SingleNameSelector
95107
sname = CS.selectsingle(selector, vecnames)
96108
snames = CS.select(selector, vecnames)
97109
@test sname == :c
@@ -101,13 +113,23 @@ using Test
101113
@test sname == :c
102114
@test snames == [:c]
103115

116+
# fallback: another selector
117+
selector = CS.selector(CS.NoneSelector())
118+
@test selector isa CS.NoneSelector
119+
104120
# shows
105121
selector = CS.selector([1, 2, 3])
106122
@test sprint(show, selector) == "[1, 2, 3]"
123+
selector = CS.selector((1, 2, 3))
124+
@test sprint(show, selector) == "[1, 2, 3]"
125+
selector = CS.selector([:a, :b, :c])
126+
@test sprint(show, selector) == "[:a, :b, :c]"
107127
selector = CS.selector((:a, :b, :c))
108128
@test sprint(show, selector) == "[:a, :b, :c]"
109129
selector = CS.selector(["a", "b", "c"])
110130
@test sprint(show, selector) == "[:a, :b, :c]"
131+
selector = CS.selector(("a", "b", "c"))
132+
@test sprint(show, selector) == "[:a, :b, :c]"
111133
selector = CS.selector(r"[abc]")
112134
@test sprint(show, selector) == "r\"[abc]\""
113135
selector = CS.selector(:)
@@ -129,6 +151,14 @@ using Test
129151
@test_throws ArgumentError CS.selector(Int[])
130152
@test_throws ArgumentError CS.selector(Symbol[])
131153
@test_throws ArgumentError CS.selector(String[])
154+
# column indices must be unique
155+
@test_throws ArgumentError CS.selector([1, 2, 2])
156+
@test_throws ArgumentError CS.selector((1, 2, 2))
157+
# column names must be unique
158+
@test_throws ArgumentError CS.selector([:a, :b, :b])
159+
@test_throws ArgumentError CS.selector((:a, :b, :b))
160+
@test_throws ArgumentError CS.selector(["a", "b", "b"])
161+
@test_throws ArgumentError CS.selector(("a", "b", "b"))
132162
# regex doesn't match any names in input table
133163
selector = CS.selector(r"x")
134164
@test_throws AssertionError CS.select(selector, vecnames)

0 commit comments

Comments
 (0)