From 7f6401595782dbbea8dc0d070b1997fb92b5149e Mon Sep 17 00:00:00 2001 From: Akos Hajdu Date: Wed, 7 Jul 2021 06:14:56 -0700 Subject: [PATCH] [erl-frontend] Add tests for binary expression translation Summary: Add some tests for binary expressions. The idea is that tests come in pairs: in both cases we do the same calculations, one is asserting the good result and one is not, where only the latter should trigger a warning. Reviewed By: rgrig Differential Revision: D29556364 fbshipit-source-id: 5b601d141 --- Makefile | 1 + .../codetoanalyze/erlang/features/Makefile | 17 +++ .../codetoanalyze/erlang/features/issues.exp | 7 + .../erlang/features/rebar.config | 11 ++ .../erlang/features/src/arithmetic.erl | 128 ++++++++++++++++++ .../erlang/features/src/features.app.src | 14 ++ 6 files changed, 178 insertions(+) create mode 100644 infer/tests/codetoanalyze/erlang/features/Makefile create mode 100644 infer/tests/codetoanalyze/erlang/features/issues.exp create mode 100644 infer/tests/codetoanalyze/erlang/features/rebar.config create mode 100644 infer/tests/codetoanalyze/erlang/features/src/arithmetic.erl create mode 100644 infer/tests/codetoanalyze/erlang/features/src/features.app.src diff --git a/Makefile b/Makefile index 636bbfc17..d64322532 100644 --- a/Makefile +++ b/Makefile @@ -153,6 +153,7 @@ ifneq ($(REBAR3),no) DIRECT_TESTS += \ erlang_nonmatch \ erlang_topl \ + erlang_features \ BUILD_SYSTEMS_TESTS += rebar3 endif diff --git a/infer/tests/codetoanalyze/erlang/features/Makefile b/infer/tests/codetoanalyze/erlang/features/Makefile new file mode 100644 index 000000000..790a5f490 --- /dev/null +++ b/infer/tests/codetoanalyze/erlang/features/Makefile @@ -0,0 +1,17 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +TESTS_DIR = ../../.. + +# see explanations in cpp/biabduction/Makefile for the custom isystem +INFER_OPTIONS = --pulse-only --debug-exceptions --project-root $(TESTS_DIR) \ + --pulse-report-latent-issues +INFERPRINT_OPTIONS = --issues-tests + +SOURCES = $(wildcard src/*.erl) + +include $(TESTS_DIR)/erlang.make + +infer-out/report.json: $(MAKEFILE_LIST) diff --git a/infer/tests/codetoanalyze/erlang/features/issues.exp b/infer/tests/codetoanalyze/erlang/features/issues.exp new file mode 100644 index 000000000..0e3399d67 --- /dev/null +++ b/infer/tests/codetoanalyze/erlang/features/issues.exp @@ -0,0 +1,7 @@ +codetoanalyze/erlang/features/src/arithmetic.erl, test_add_Bad/0, -9, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `warn/1`,no pattern match here] +codetoanalyze/erlang/features/src/arithmetic.erl, test_idiv1_Bad/0, -54, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `warn/1`,no pattern match here] +codetoanalyze/erlang/features/src/arithmetic.erl, test_mul_Bad/0, -39, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `warn/1`,no pattern match here] +codetoanalyze/erlang/features/src/arithmetic.erl, test_multiple_Bad/0, -98, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `warn/1`,no pattern match here] +codetoanalyze/erlang/features/src/arithmetic.erl, test_rem_Bad/0, -85, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `warn/1`,no pattern match here] +codetoanalyze/erlang/features/src/arithmetic.erl, test_sub_Bad/0, -24, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [calling context starts here,in call to `warn/1`,no pattern match here] +codetoanalyze/erlang/features/src/arithmetic.erl, warn/1, 0, NONEXHAUSTIVE_PATTERN_MATCH, no_bucket, ERROR, [*** LATENT ***,no pattern match here] diff --git a/infer/tests/codetoanalyze/erlang/features/rebar.config b/infer/tests/codetoanalyze/erlang/features/rebar.config new file mode 100644 index 000000000..b041a0509 --- /dev/null +++ b/infer/tests/codetoanalyze/erlang/features/rebar.config @@ -0,0 +1,11 @@ +% Copyright (c) Facebook, Inc. and its affiliates. +% +% This source code is licensed under the MIT license found in the +% LICENSE file in the root directory of this source tree. + +{erl_opts, []}. +{deps, []}. + +{shell, [ + {apps, [translation]} +]}. diff --git a/infer/tests/codetoanalyze/erlang/features/src/arithmetic.erl b/infer/tests/codetoanalyze/erlang/features/src/arithmetic.erl new file mode 100644 index 000000000..722359188 --- /dev/null +++ b/infer/tests/codetoanalyze/erlang/features/src/arithmetic.erl @@ -0,0 +1,128 @@ +% Copyright (c) Facebook, Inc. and its affiliates. +% +% This source code is licensed under the MIT license found in the +% LICENSE file in the root directory of this source tree. + +-module(arithmetic). + +-export([ + test_add_Ok/0, + test_add_Bad/0, + test_sub_Ok/0, + test_sub_Bad/0, + test_mul_Ok/0, + test_mul_Bad/0, + test_idiv1_Ok/0, + test_idiv1_Bad/0, + test_idiv2_Ok/0, + fn_test_idiv2_Bad/0, + test_rem_Ok/0, + test_rem_Bad/0, + test_multiple_Ok/0, + test_multiple_Bad/0 +]). + +% Call this method with warn(1) to trigger a warning to expect +warn(0) -> ok. + +test_add_Ok() -> + X = 2, + Y = 3, + case X + Y of + 5 -> ok; + _ -> warn(1) + end. +test_add_Bad() -> + X = 2, + Y = 3, + case X + Y of + 5 -> warn(1); + _ -> ok + end. + +test_sub_Ok() -> + X = 5, + Y = 3, + case X - Y of + 2 -> ok; + _ -> warn(1) + end. +test_sub_Bad() -> + X = 5, + Y = 3, + case X - Y of + 2 -> warn(1); + _ -> ok + end. + +test_mul_Ok() -> + X = 5, + Y = 3, + case X * Y of + 15 -> ok; + _ -> warn(1) + end. +test_mul_Bad() -> + X = 5, + Y = 3, + case X * Y of + 15 -> warn(1); + _ -> ok + end. + +test_idiv1_Ok() -> + X = 21, + Y = 3, + case X div Y of + 7 -> ok; + _ -> warn(1) + end. +test_idiv1_Bad() -> + X = 21, + Y = 3, + case X div Y of + 7 -> warn(1); + _ -> ok + end. + +test_idiv2_Ok() -> + X = 22, + Y = 3, + case X div Y of + 7 -> ok; + _ -> warn(1) + end. +% FN (T94972453) +fn_test_idiv2_Bad() -> + X = 22, + Y = 3, + case X div Y of + 7 -> warn(1); + _ -> ok + end. + +test_rem_Ok() -> + X = 5, + Y = 3, + case X rem Y of + 2 -> ok; + _ -> warn(1) + end. +test_rem_Bad() -> + X = 5, + Y = 3, + case X rem Y of + 2 -> warn(1); + _ -> ok + end. + +test_multiple_Ok() -> + case (8 + 4) div 2 * 5 of + 30 -> ok; + _ -> warn(1) + end. +test_multiple_Bad() -> + case (8 + 4) div 2 * 5 of + 30 -> warn(1); + _ -> ok + end. diff --git a/infer/tests/codetoanalyze/erlang/features/src/features.app.src b/infer/tests/codetoanalyze/erlang/features/src/features.app.src new file mode 100644 index 000000000..402b33f27 --- /dev/null +++ b/infer/tests/codetoanalyze/erlang/features/src/features.app.src @@ -0,0 +1,14 @@ +% Copyright (c) Facebook, Inc. and its affiliates. +% +% This source code is licensed under the MIT license found in the +% LICENSE file in the root directory of this source tree. +{application, features, [ + {description, "An Erlang app for testing various language features"}, + {vsn, "0.1.0"}, + {registered, []}, + {applications, [ + kernel, + stdlib + ]}, + {modules, []} +]}.