File: spec_helper.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (89 lines) | stat: -rw-r--r-- 1,857 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def it_acts_as_digest_algorithm(type : T.class) forall T
  it "#hexdigest can update within a loop from explicit expr (#9483)" do
    i = 0
    type.hexdigest do |digest|
      while i < 3
        digest.update("")
        i += 1
      end
    end
  end

  it "#hexdigest can update within a loop by indirect expr (#9483)" do
    algorithm = {} of String => ::Digest::ClassMethods
    algorithm["me"] = type
    i = 0
    algorithm["me"].hexdigest do |digest|
      while i < 3
        digest.update("")
        i += 1
      end
    end
  end

  it "context are independent" do
    algorithm = type
    res = algorithm.hexdigest do |digest|
      digest.update("a")
      digest.update("b")
    end

    inner_res = nil

    outer_res = algorithm.hexdigest do |outer|
      outer.update("a")

      inner_res = algorithm.hexdigest do |inner|
        inner.update("a")
        inner.update("b")
      end

      outer.update("b")
    end

    outer_res.should eq(res)
    inner_res.should eq(res)
  end

  describe ".dup" do
    it "preserves type" do
      type.new.dup.class.should eq(type)
    end

    it "preserves value" do
      digest1 = type.new
      digest1.update("a")
      digest2 = digest1.dup

      digest1.final.should eq(digest2.final)
    end

    it "leads to not sharing state" do
      digest1 = type.new
      digest1.update("a")

      digest2 = digest1.dup

      digest1.update("b")

      digest1.final.should_not eq(digest2.final)
    end

    it "leads to deterministic updates" do
      digest1 = type.new
      digest1.update("a")

      digest2 = digest1.dup

      digest1.update("b")
      digest2.update("b")

      digest1.final.should eq(digest2.final)
    end
  end

  it "digest with file content" do
    path = datapath("test_file.txt")
    type.new.file(path).final.should eq(type.digest(File.read(path)))
  end
end