Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

THREESCALE-8483 + THREESCALE-8484 - fix for the "contains" operator #15

Merged
merged 1 commit into from Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/liquid.lua
Expand Up @@ -2128,13 +2128,11 @@ function Interpreter:visit_BinOp( node )
local right_value = self:visit(node.right)
if type(right_value) == "string" then
if type(left_value) == "string" then
return string.find(left_value, left_value)
return string.find(left_value, right_value)
elseif type(left_value) == "table" then
for i, v in ipairs(left_value) do
if type(v) == "string" then
if string.find(v, right_value) then
return true
end
if type(v) == "string" and v == right_value then
return true
end
end
return false
Expand Down
102 changes: 102 additions & 0 deletions t/operators.t
@@ -0,0 +1,102 @@
use Test::Nginx::Socket::Lua;
use Cwd qw(cwd);

plan tests => repeat_each() * (3 * blocks());

my $pwd = cwd();

our $HttpConfig = qq{
lua_package_path "$pwd/lib/?.lua;;;";
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
};

no_long_string();
run_tests();

__DATA__

=== TEST 1: 'contains' operator.
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local Liquid = require 'liquid'
local Lexer = Liquid.Lexer
local Parser = Liquid.Parser
local Interpreter = Liquid.Interpreter
local document = "{% if 'abc' contains 'def' %}yes{% else %}no {% endif %}"..
"{% if 'abcd' contains 'bc' %}yes {% else %}no{% endif %}"..
"{% assign var = 'abc afooa ghi' | split: ' ' %}{% if var contains 'foo' %}yes{% else %}no {% endif %}"..
"{% assign var = 'abc def ghi' | split: ' ' %}{% if var contains 'def' %}yes {% else %}no{% endif %}"
local lexer = Lexer:new(document)
local parser = Parser:new(lexer)
local interpreter = Interpreter:new(parser)
ngx.say(interpreter:interpret())
}
}
--- request
GET /t
--- response_body
no yes no yes
--- no_error_log
[error]

=== TEST 2: '==' operator.
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local Liquid = require 'liquid'
local Lexer = Liquid.Lexer
local Parser = Liquid.Parser
local Interpreter = Liquid.Interpreter
local document = "{% if 'abc' == 'def' %}yes{% else %}no {% endif %}"..
"{% if 'abcd' == 'abcd' %}yes {% else %}no{% endif %}"..
"{% assign var = 'abc def' | split: ' ' %}"..
"{% assign var2 = 'abc def ghi' | split: ' ' %}"..
"{% if var == var2 %}yes{% else %}no {% endif %}"..
"{% assign var = 'abc def ghi' | split: ' ' %}"..
"{% assign var2 = 'abc def ghi' | split: ' ' %}"..
"{% if var == var2 %}yes {% else %}no{% endif %}"
local lexer = Lexer:new(document)
local parser = Parser:new(lexer)
local interpreter = Interpreter:new(parser)
ngx.say(interpreter:interpret())
}
}
--- request
GET /t
--- response_body
no yes no yes
--- no_error_log
[error]

=== TEST 3: '!=' operator.
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
local Liquid = require 'liquid'
local Lexer = Liquid.Lexer
local Parser = Liquid.Parser
local Interpreter = Liquid.Interpreter
local document = "{% if 'abc' != 'def' %}yes {% else %}no{% endif %}"..
"{% if 'abcd' != 'abcd' %}yes{% else %}no {% endif %}"..
"{% assign var = 'abc def' | split: ' ' %}"..
"{% assign var2 = 'abc def ghi' | split: ' ' %}"..
"{% if var != var2 %}yes {% else %}no{% endif %}"..
"{% assign var = 'abc def ghi' | split: ' ' %}"..
"{% assign var2 = 'abc def ghi' | split: ' ' %}"..
"{% if var != var2 %}yes{% else %}no {% endif %}"
local lexer = Lexer:new(document)
local parser = Parser:new(lexer)
local interpreter = Interpreter:new(parser)
ngx.say(interpreter:interpret())
}
}
--- request
GET /t
--- response_body
yes no yes no
--- no_error_log
[error]