File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,8 @@ class Json
5
5
6
6
def initialize ( filename )
7
7
@filename = filename
8
- @path = Pathname . new ( File . join ( File . dirname ( __FILE__ ) , "../../../cache" , @filename ) ) . cleanpath
8
+ raise ArgumentError , "Invalid cache file: #{ filename } " unless %w[ length.json weight.json volume.json test.json ] . include? ( filename . to_s )
9
+ @path = Pathname . new ( File . join ( File . dirname ( __FILE__ ) , "../../../cache" , filename ) ) . cleanpath
9
10
end
10
11
11
12
def exist?
Original file line number Diff line number Diff line change @@ -14,6 +14,24 @@ class Measured::Cache::JsonTest < ActiveSupport::TestCase
14
14
refute_match "../" , @cache . path . to_s
15
15
end
16
16
17
+ test "#initialize accepts valid cache filenames for Measured::Measurable.subclasses, and test file" do
18
+ valid_files = %w[ length.json weight.json volume.json test.json ]
19
+ valid_files . each do |filename |
20
+ assert_nothing_raised do
21
+ Measured ::Cache ::Json . new ( filename )
22
+ end
23
+ end
24
+ end
25
+
26
+ test "#initialize rejects invalid cache filenames" do
27
+ invalid_files = [ "volum.json" , "../volume.json" , "other.txt" ]
28
+ invalid_files . each do |filename |
29
+ assert_raises ArgumentError do
30
+ Measured ::Cache ::Json . new ( filename )
31
+ end
32
+ end
33
+ end
34
+
17
35
test "#exist? returns false if the file does not exist" do
18
36
File . expects ( :exist? ) . with ( @cache . path ) . returns ( false )
19
37
refute_predicate @cache , :exist?
You can’t perform that action at this time.
0 commit comments