|
1 | | -# encoding: utf-8 |
2 | | - |
3 | | -require 'spec_helper' |
4 | | - |
5 | | -describe DetectLanguage do |
| 1 | +# frozen_string_literal: true |
6 | 2 |
|
| 3 | +RSpec.describe DetectLanguage do |
7 | 4 | let(:api_key) { ENV['DETECTLANGUAGE_API_KEY'] } |
| 5 | + let(:secure) { true } |
8 | 6 |
|
9 | 7 | 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 |
19 | 10 | end |
20 | 11 |
|
21 | | - context 'invalid api key' do |
22 | | - let(:api_key) {'invalid'} |
| 12 | + describe '.configuration' do |
| 13 | + subject { described_class.configuration } |
23 | 14 |
|
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}") |
28 | 19 | end |
29 | 20 | end |
30 | 21 |
|
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) } |
35 | 24 |
|
36 | | - result = subject.detect("Jau saulelė vėl atkopdama budino svietą") |
37 | | - result[0]['language'].should == "lt" |
38 | | - end |
| 25 | + let(:query) { 'Hello world' } |
39 | 26 |
|
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') |
42 | 31 | end |
43 | 32 |
|
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ą' } |
46 | 35 |
|
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 |
49 | 39 | end |
50 | 40 |
|
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ą'] } |
53 | 43 |
|
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 |
57 | 51 | end |
58 | 52 |
|
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' } |
62 | 55 |
|
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) |
66 | 58 | end |
67 | 59 | end |
68 | 60 | end |
69 | 61 |
|
| 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 | + |
70 | 72 | describe '.user_status' do |
71 | | - subject(:user_status) {DetectLanguage.user_status} |
| 73 | + subject { DetectLanguage.user_status } |
72 | 74 |
|
73 | 75 | 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 | + ) |
80 | 84 | end |
81 | 85 | end |
82 | 86 |
|
83 | 87 | describe '.languages' do |
84 | | - subject(:languages) {DetectLanguage.languages} |
| 88 | + subject { DetectLanguage.languages } |
85 | 89 |
|
86 | 90 | 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 |
88 | 100 | end |
89 | 101 | end |
90 | 102 | end |
0 commit comments