File: connection_spec.rb

package info (click to toggle)
ruby-vips 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,164 kB
  • sloc: ruby: 3,530; makefile: 3
file content (160 lines) | stat: -rw-r--r-- 4,461 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require "spec_helper"

RSpec.describe Vips::Source, version: [8, 9] do
  it "can create a source from a descriptor" do
    source = Vips::Source.new_from_descriptor(0)

    expect(source)
  end

  it "can create a source from a filename" do
    source = Vips::Source.new_from_file simg("wagon.jpg")

    expect(source)
  end

  it "can't create a source from a bad filename" do
    expect {
      Vips::Source.new_from_file simg("banana.jpg")
    }.to raise_exception(Vips::Error)
  end

  it "can create a source from an area of memory" do
    str = File.open(simg("wagon.jpg"), "rb").read
    source = Vips::Source.new_from_memory str

    expect(source)
  end

  it "sources have filenames and nicks" do
    source = Vips::Source.new_from_file simg("wagon.jpg")

    expect(source.filename).to eq(simg("wagon.jpg"))
    expect(source.nick)
  end

  it "can load an image from filename source" do
    source = Vips::Source.new_from_file simg("wagon.jpg")
    image = Vips::Image.new_from_source source, ""

    expect(image)
    expect(image.width).to eq(685)
    expect(image.height).to eq(478)
    expect(image.bands).to eq(3)
    expect(image.avg).to be_within(0.001).of(109.789)
  end
end

RSpec.describe Vips::Target, version: [8, 9] do
  it "can create a target to a filename" do
    target = Vips::Target.new_to_file timg("x.jpg")

    expect(target)
  end

  it "can create a target to a descriptor" do
    target = Vips::Target.new_to_descriptor 1

    expect(target)
  end

  it "can create a target to a memory area" do
    target = Vips::Target.new_to_memory

    expect(target)
  end

  it "can't create a target to a bad filename" do
    expect {
      Vips::Target.new_to_file "/banana/monkey"
    }.to raise_exception(Vips::Error)
  end

  it "can save an image to a filename target" do
    source = Vips::Source.new_from_file simg("wagon.jpg")
    image = Vips::Image.new_from_source source, ""
    filename = timg("x4.png")
    target = Vips::Target.new_to_file filename
    image.write_to_target target, ".png"

    image = Vips::Image.new_from_file filename
    expect(image)
    expect(image.width).to eq(685)
    expect(image.height).to eq(478)
    expect(image.bands).to eq(3)
    expect(image.avg).to be_within(0.001).of(109.789)
  end

  it "can save an image to a memory target" do
    source = Vips::Source.new_from_file simg("wagon.jpg")
    image = Vips::Image.new_from_source source, ""
    target = Vips::Target.new_to_memory
    image.write_to_target target, ".png"
    memory = target.get("blob")

    image = Vips::Image.new_from_buffer memory, ""
    expect(image)
    expect(image.width).to eq(685)
    expect(image.height).to eq(478)
    expect(image.bands).to eq(3)
    expect(image.avg).to be_within(0.001).of(109.789)
  end
end

RSpec.describe Vips::SourceCustom, version: [8, 9] do
  it "can create a custom source" do
    source = Vips::SourceCustom.new

    expect(source)
  end

  it "can load a custom source" do
    file = File.open simg("wagon.jpg"), "rb"
    source = Vips::SourceCustom.new
    source.on_read { |length| file.read length }
    source.on_seek { |offset, whence| file.seek(offset, whence) }
    image = Vips::Image.new_from_source source, ""

    expect(image)
    expect(image.width).to eq(685)
    expect(image.height).to eq(478)
    expect(image.bands).to eq(3)
    expect(image.avg).to be_within(0.001).of(109.789)
  end

  it "on_seek is optional" do
    file = File.open simg("wagon.jpg"), "rb"
    source = Vips::SourceCustom.new
    source.on_read { |length| file.read length }
    image = Vips::Image.new_from_source source, ""

    expect(image)
    expect(image.width).to eq(685)
    expect(image.height).to eq(478)
    expect(image.bands).to eq(3)
    expect(image.avg).to be_within(0.001).of(109.789)
  end

  it "can create a user output stream" do
    target = Vips::TargetCustom.new

    expect(target)
  end

  it "can write an image to a user output stream" do
    filename = timg("x5.png")
    file = File.open filename, "wb"
    target = Vips::TargetCustom.new
    target.on_write { |chunk| file.write(chunk) }
    target.on_finish { file.close }
    image = Vips::Image.new_from_file simg("wagon.jpg")
    image.write_to_target target, ".png"

    image = Vips::Image.new_from_file filename
    expect(image)
    expect(image.width).to eq(685)
    expect(image.height).to eq(478)
    expect(image.bands).to eq(3)
    expect(image.avg).to be_within(0.5).of(109.789)
  end
end