Skip to content

Commit 7dbb5c5

Browse files
authored
Merge pull request #15 from 3scale/THREESCALE-8483_THREESCALE-8484
THREESCALE-8483 + THREESCALE-8484 - fix for the "contains" operator
2 parents fcd4237 + 8ab3fdb commit 7dbb5c5

2 files changed

Lines changed: 105 additions & 5 deletions

File tree

lib/liquid.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,13 +2128,11 @@ function Interpreter:visit_BinOp( node )
21282128
local right_value = self:visit(node.right)
21292129
if type(right_value) == "string" then
21302130
if type(left_value) == "string" then
2131-
return string.find(left_value, left_value)
2131+
return string.find(left_value, right_value)
21322132
elseif type(left_value) == "table" then
21332133
for i, v in ipairs(left_value) do
2134-
if type(v) == "string" then
2135-
if string.find(v, right_value) then
2136-
return true
2137-
end
2134+
if type(v) == "string" and v == right_value then
2135+
return true
21382136
end
21392137
end
21402138
return false

t/operators.t

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use Test::Nginx::Socket::Lua;
2+
use Cwd qw(cwd);
3+
4+
plan tests => repeat_each() * (3 * blocks());
5+
6+
my $pwd = cwd();
7+
8+
our $HttpConfig = qq{
9+
lua_package_path "$pwd/lib/?.lua;;;";
10+
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
11+
};
12+
13+
no_long_string();
14+
run_tests();
15+
16+
__DATA__
17+
18+
=== TEST 1: 'contains' operator.
19+
--- http_config eval: $::HttpConfig
20+
--- config
21+
location /t {
22+
content_by_lua_block {
23+
local Liquid = require 'liquid'
24+
local Lexer = Liquid.Lexer
25+
local Parser = Liquid.Parser
26+
local Interpreter = Liquid.Interpreter
27+
local document = "{% if 'abc' contains 'def' %}yes{% else %}no {% endif %}"..
28+
"{% if 'abcd' contains 'bc' %}yes {% else %}no{% endif %}"..
29+
"{% assign var = 'abc afooa ghi' | split: ' ' %}{% if var contains 'foo' %}yes{% else %}no {% endif %}"..
30+
"{% assign var = 'abc def ghi' | split: ' ' %}{% if var contains 'def' %}yes {% else %}no{% endif %}"
31+
local lexer = Lexer:new(document)
32+
local parser = Parser:new(lexer)
33+
local interpreter = Interpreter:new(parser)
34+
ngx.say(interpreter:interpret())
35+
}
36+
}
37+
--- request
38+
GET /t
39+
--- response_body
40+
no yes no yes
41+
--- no_error_log
42+
[error]
43+
44+
=== TEST 2: '==' operator.
45+
--- http_config eval: $::HttpConfig
46+
--- config
47+
location /t {
48+
content_by_lua_block {
49+
local Liquid = require 'liquid'
50+
local Lexer = Liquid.Lexer
51+
local Parser = Liquid.Parser
52+
local Interpreter = Liquid.Interpreter
53+
local document = "{% if 'abc' == 'def' %}yes{% else %}no {% endif %}"..
54+
"{% if 'abcd' == 'abcd' %}yes {% else %}no{% endif %}"..
55+
"{% assign var = 'abc def' | split: ' ' %}"..
56+
"{% assign var2 = 'abc def ghi' | split: ' ' %}"..
57+
"{% if var == var2 %}yes{% else %}no {% endif %}"..
58+
"{% assign var = 'abc def ghi' | split: ' ' %}"..
59+
"{% assign var2 = 'abc def ghi' | split: ' ' %}"..
60+
"{% if var == var2 %}yes {% else %}no{% endif %}"
61+
local lexer = Lexer:new(document)
62+
local parser = Parser:new(lexer)
63+
local interpreter = Interpreter:new(parser)
64+
ngx.say(interpreter:interpret())
65+
}
66+
}
67+
--- request
68+
GET /t
69+
--- response_body
70+
no yes no yes
71+
--- no_error_log
72+
[error]
73+
74+
=== TEST 3: '!=' operator.
75+
--- http_config eval: $::HttpConfig
76+
--- config
77+
location /t {
78+
content_by_lua_block {
79+
local Liquid = require 'liquid'
80+
local Lexer = Liquid.Lexer
81+
local Parser = Liquid.Parser
82+
local Interpreter = Liquid.Interpreter
83+
local document = "{% if 'abc' != 'def' %}yes {% else %}no{% endif %}"..
84+
"{% if 'abcd' != 'abcd' %}yes{% else %}no {% endif %}"..
85+
"{% assign var = 'abc def' | split: ' ' %}"..
86+
"{% assign var2 = 'abc def ghi' | split: ' ' %}"..
87+
"{% if var != var2 %}yes {% else %}no{% endif %}"..
88+
"{% assign var = 'abc def ghi' | split: ' ' %}"..
89+
"{% assign var2 = 'abc def ghi' | split: ' ' %}"..
90+
"{% if var != var2 %}yes{% else %}no {% endif %}"
91+
local lexer = Lexer:new(document)
92+
local parser = Parser:new(lexer)
93+
local interpreter = Interpreter:new(parser)
94+
ngx.say(interpreter:interpret())
95+
}
96+
}
97+
--- request
98+
GET /t
99+
--- response_body
100+
yes no yes no
101+
--- no_error_log
102+
[error]

0 commit comments

Comments
 (0)