-module(core_banned_word_tests). -include_lib("eunit/include/eunit.hrl"). -include("records.hrl"). setup() -> mnesia:start(), mnesia:create_table(banned_word, [ {attributes, record_info(fields, banned_word)}, {ram_copies, [node()]} ]), ok. cleanup(_) -> mnesia:delete_table(banned_word), mnesia:stop(), ok. core_banned_word_test_() -> {foreach, fun setup/0, fun cleanup/1, [ {"Add banned word test", fun test_add_word/0}, {"Add duplicate word test", fun test_add_duplicate/0}, {"Remove banned word test", fun test_remove_word/0}, {"List banned words test", fun test_list_words/0}, {"Is banned test", fun test_is_banned/0}, {"Check text test", fun test_check_text/0}, {"Filter text test", fun test_filter_text/0} ]}. test_add_word() -> Word = <<"badword">>, {ok, BannedWord} = core_banned_word:add(Word), ?assertEqual(Word, BannedWord#banned_word.word), ?assert(is_binary(BannedWord#banned_word.id)). test_add_duplicate() -> Word = <<"badword">>, {ok, _} = core_banned_word:add(Word), {error, already_exists} = core_banned_word:add(Word), {error, already_exists} = core_banned_word:add(<<"BADWORD">>). % case insensitive test_remove_word() -> Word = <<"badword">>, {ok, _} = core_banned_word:add(Word), {ok, removed} = core_banned_word:remove(Word), {error, not_found} = core_banned_word:remove(<<"nonexistent">>). test_list_words() -> {ok, _} = core_banned_word:add(<<"word1">>), {ok, _} = core_banned_word:add(<<"word2">>), {ok, _} = core_banned_word:add(<<"word3">>), {ok, Words} = core_banned_word:list_all(), ?assertEqual(3, length(Words)), ?assert(lists:member(<<"word1">>, Words)). test_is_banned() -> Word = <<"badword">>, ?assertNot(core_banned_word:is_banned(Word)), {ok, _} = core_banned_word:add(Word), ?assert(core_banned_word:is_banned(Word)), ?assert(core_banned_word:is_banned(<<"BADWORD">>)). % case insensitive test_check_text() -> {ok, _} = core_banned_word:add(<<"bad">>), {ok, _} = core_banned_word:add(<<"spam">>), ?assertNot(core_banned_word:check_text(<<"Hello world">>)), ?assert(core_banned_word:check_text(<<"This is bad">>)), ?assert(core_banned_word:check_text(<<"This is SPAM">>)). test_filter_text() -> {ok, _} = core_banned_word:add(<<"bad">>), {ok, _} = core_banned_word:add(<<"spam">>), ?assertEqual(<<"Hello world">>, core_banned_word:filter_text(<<"Hello world">>)), ?assertEqual(<<"This is ***">>, core_banned_word:filter_text(<<"This is bad">>)), ?assertEqual(<<"*** and ***">>, core_banned_word:filter_text(<<"bad and spam">>)).