Skip to content

Commit fde2a52

Browse files
authored
Merge pull request #9 from detectlanguage/feature/update-specs
Update specs
2 parents e6b0854 + c35e3d8 commit fde2a52

4 files changed

Lines changed: 70 additions & 53 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
*.gem
22
*.rbc
3+
.env.local
34
.bundle
45
.config
56
.yardoc

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
--color
22
--format progress
3+
--require spec_helper

mise.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[tools]
22
ruby = "3.4.5"
3+
4+
[env]
5+
_.file = ".env.local"

spec/detect_language_spec.rb

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,102 @@
1-
# encoding: utf-8
2-
3-
require 'spec_helper'
4-
5-
describe DetectLanguage do
1+
# frozen_string_literal: true
62

3+
RSpec.describe DetectLanguage do
74
let(:api_key) { ENV['DETECTLANGUAGE_API_KEY'] }
5+
let(:secure) { true }
86

97
before do
10-
DetectLanguage.configuration.api_key = api_key
11-
end
12-
13-
context "configuration" do
14-
it "should have default configuration values" do
15-
subject.configuration.api_version.should == '0.2'
16-
subject.configuration.host.should == 'ws.detectlanguage.com'
17-
subject.configuration.user_agent.should == "detectlanguage-ruby/#{DetectLanguage::VERSION}"
18-
end
8+
described_class.configuration.api_key = api_key
9+
described_class.configuration.secure = secure
1910
end
2011

21-
context 'invalid api key' do
22-
let(:api_key) {'invalid'}
12+
describe '.configuration' do
13+
subject { described_class.configuration }
2314

24-
it "should raise exception for invalid key" do
25-
lambda {
26-
subject.detect("Hello world")
27-
}.should raise_error(DetectLanguage::Error)
15+
it 'has default configuration values' do
16+
expect(subject.api_version).to eq('0.2')
17+
expect(subject.host).to eq('ws.detectlanguage.com')
18+
expect(subject.user_agent).to eq("detectlanguage-ruby/#{DetectLanguage::VERSION}")
2819
end
2920
end
3021

31-
context "detection" do
32-
it "should detect languages" do
33-
result = subject.detect("Hello world")
34-
result[0]['language'].should == "en"
22+
describe '.detect' do
23+
subject { described_class.detect(query) }
3524

36-
result = subject.detect("Jau saulelė vėl atkopdama budino svietą")
37-
result[0]['language'].should == "lt"
38-
end
25+
let(:query) { 'Hello world' }
3926

40-
it "should have simple way to detect a language" do
41-
subject.simple_detect("Hello world").should == "en"
27+
it 'detects language' do
28+
expect(subject).to be_an(Array)
29+
expect(subject.first).to be_a(Hash)
30+
expect(subject.first['language']).to eq('en')
4231
end
4332

44-
it "should allow sending batch requests" do
45-
result = subject.detect(["Hello world", "Jau saulelė vėl atkopdama budino svietą"])
33+
context 'with unicode characters' do
34+
let(:query) { 'Jau saulelė vėl atkopdama budino svietą' }
4635

47-
result[0][0]['language'].should == "en"
48-
result[1][0]['language'].should == "lt"
36+
it 'detects language with unicode characters' do
37+
expect(subject.first['language']).to eq('lt')
38+
end
4939
end
5040

51-
it "with empty text in the batch it detects correctly" do
52-
result = subject.detect(["", "Hello", "Jau saulelė vėl atkopdama budino svietą"])
41+
context 'with batch requests' do
42+
let(:query) { ['', 'Hello world', 'Jau saulelė vėl atkopdama budino svietą'] }
5343

54-
result[0].should be_empty
55-
result[1][0]['language'].should == "en"
56-
result[2][0]['language'].should == "lt"
44+
it 'detects languages in batch' do
45+
expect(subject).to be_an(Array)
46+
expect(subject.size).to eq(3)
47+
expect(subject[0]).to be_empty
48+
expect(subject[1][0]['language']).to eq('en')
49+
expect(subject[2][0]['language']).to eq('lt')
50+
end
5751
end
5852

59-
context 'secure mode' do
60-
before { DetectLanguage.configuration.secure = true }
61-
after { DetectLanguage.configuration.secure = false }
53+
context 'invalid api key' do
54+
let(:api_key) { 'invalid' }
6255

63-
it "detects language" do
64-
result = subject.detect("Hello world")
65-
result[0]['language'].should == "en"
56+
it "should raise exception for invalid key" do
57+
expect { subject }.to raise_error(DetectLanguage::Error)
6658
end
6759
end
6860
end
6961

62+
describe '.simple_detect' do
63+
subject { described_class.simple_detect(query) }
64+
65+
let(:query) { 'Hello world' }
66+
67+
it 'detects language' do
68+
expect(subject).to eq('en')
69+
end
70+
end
71+
7072
describe '.user_status' do
71-
subject(:user_status) {DetectLanguage.user_status}
73+
subject { DetectLanguage.user_status }
7274

7375
it 'fetches user status' do
74-
expect(user_status['date']).to be_kind_of(String)
75-
expect(user_status['requests']).to be_kind_of(Integer)
76-
expect(user_status['bytes']).to be_kind_of(Integer)
77-
expect(user_status['plan']).to be_kind_of(String)
78-
expect(user_status['daily_requests_limit']).to be_kind_of(Integer)
79-
expect(user_status['daily_bytes_limit']).to be_kind_of(Integer)
76+
expect(subject).to include(
77+
'date' => kind_of(String),
78+
'requests' => kind_of(Integer),
79+
'bytes' => kind_of(Integer),
80+
'plan' => kind_of(String),
81+
'daily_requests_limit' => kind_of(Integer),
82+
'daily_bytes_limit' => kind_of(Integer),
83+
)
8084
end
8185
end
8286

8387
describe '.languages' do
84-
subject(:languages) {DetectLanguage.languages}
88+
subject { DetectLanguage.languages }
8589

8690
it 'fetches list of detectable languages' do
87-
expect(languages).to include({ 'code' => 'en', 'name' => 'ENGLISH' })
91+
expect(subject).to include('code' => 'en', 'name' => 'ENGLISH')
92+
end
93+
94+
context 'with http' do
95+
let(:secure) { false }
96+
97+
it 'fetches languages over http' do
98+
expect(subject).to include('code' => 'en', 'name' => 'ENGLISH')
99+
end
88100
end
89101
end
90102
end

0 commit comments

Comments
 (0)