<s> from __future__ import unicode_literals <EOL> import re <EOL> import pickle <EOL> import unittest <EOL> from . . atoms import String , RegExp , IP , DomainName <EOL> from . . rules import And , Or , No , Match , NonMatch , Fuzzy <EOL> from ... events import Event <EOL> class TestRules ( unittest . TestCase ) : <EOL> def test_results_should_get_cached ( self ) : <EOL> rule = Match ( "<STR_LIT:a>" , "<STR_LIT:a>" ) <EOL> cache = { } <EOL> rule . match ( Event ( ) , cache ) <EOL> self . assertTrue ( rule in cache ) <EOL> self . assertFalse ( cache [ rule ] ) <EOL> def test_cached_results_should_get_used ( self ) : <EOL> rule = Match ( "<STR_LIT:a>" , "<STR_LIT:a>" ) <EOL> cache = { rule : True } <EOL> self . assertTrue ( rule . match ( Event ( ) , cache ) ) <EOL> class TestAnd ( unittest . TestCase ) : <EOL> def test_can_not_be_initialized_with_zero_arguments ( self ) : <EOL> self . assertRaises ( TypeError , And ) <EOL> def test_commutativity ( self ) : <EOL> a = Match ( "<STR_LIT:a>" , "<STR_LIT:a>" ) <EOL> b = Match ( "<STR_LIT:b>" , "<STR_LIT:b>" ) <EOL> self . assertEqual ( And ( a , b ) , And ( b , a ) ) <EOL> def test_redundant_arguments_get_deduplicated ( self ) : <EOL> a = Match ( "<STR_LIT:a>" , "<STR_LIT:a>" ) <EOL> self . assertEqual ( And ( a , a ) , And ( a ) ) <EOL> _options = [ <EOL> And ( Match ( "<STR_LIT:a>" ) , Match ( "<STR_LIT:b>" ) ) <EOL> ] <EOL> def test_pickling_and_unpickling ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , pickle . loads ( pickle . dumps ( option ) ) ) <EOL> def test_repr ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , eval ( repr ( option ) ) ) <EOL> class TestOr ( unittest . TestCase ) : <EOL> def test_can_not_be_initialized_with_zero_arguments ( self ) : <EOL> self . assertRaises ( TypeError , Or ) <EOL> def test_commutativity ( self ) : <EOL> a = Match ( "<STR_LIT:a>" , "<STR_LIT:a>" ) <EOL> b = Match ( "<STR_LIT:b>" , "<STR_LIT:b>" ) <EOL> self . assertEqual ( Or ( a , b ) , Or ( b , a ) ) <EOL> def test_redundant_arguments_get_deduplicated ( self ) : <EOL> a = Match ( "<STR_LIT:a>" , "<STR_LIT:a>" ) <EOL> self . assertEqual ( Or ( a , a ) , Or ( a ) ) <EOL> _options = [ <EOL> Or ( Match ( "<STR_LIT:a>" ) , Match ( "<STR_LIT:b>" ) ) <EOL> ] <EOL> def test_pickling_and_unpickling ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , pickle . loads ( pickle . dumps ( option ) ) ) <EOL> def test_repr ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , eval ( repr ( option ) ) ) <EOL> class TestNo ( unittest . TestCase ) : <EOL> _options = [ <EOL> No ( Match ( "<STR_LIT:a>" ) ) <EOL> ] <EOL> def test_pickling_and_unpickling ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , pickle . loads ( pickle . dumps ( option ) ) ) <EOL> def test_repr ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , eval ( repr ( option ) ) ) <EOL> class TestMatch ( unittest . TestCase ) : <EOL> def test_init_conversions ( self ) : <EOL> self . assertEqual ( <EOL> Match ( "<STR_LIT:a>" , "<STR_LIT:b>" ) , <EOL> Match ( "<STR_LIT:a>" , String ( "<STR_LIT:b>" ) ) ) <EOL> self . assertEqual ( <EOL> Match ( "<STR_LIT:a>" , re . compile ( "<STR_LIT:b>" ) ) , <EOL> Match ( "<STR_LIT:a>" , RegExp ( "<STR_LIT:b>" ) ) ) <EOL> def test_domainname ( self ) : <EOL> matcher = Match ( "<STR_LIT:a>" , DomainName ( "<STR_LIT>" ) ) <EOL> self . assertTrue ( matcher . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> self . assertTrue ( matcher . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> self . assertFalse ( matcher . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> self . assertFalse ( matcher . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> self . assertFalse ( matcher . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> _options = [ <EOL> Match ( ) , <EOL> Match ( "<STR_LIT:a>" , String ( "<STR_LIT:b>" ) ) , <EOL> Match ( "<STR_LIT:a>" , RegExp ( "<STR_LIT:b>" ) ) , <EOL> Match ( "<STR_LIT:a>" , IP ( "<STR_LIT>" ) ) , <EOL> Match ( "<STR_LIT:a>" , DomainName ( "<STR_LIT>" ) ) <EOL> ] <EOL> def test_pickling_and_unpickling ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , pickle . loads ( pickle . dumps ( option ) ) ) <EOL> def test_repr ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , eval ( repr ( option ) ) ) <EOL> class TestNonMatch ( unittest . TestCase ) : <EOL> def test_init_conversions ( self ) : <EOL> self . assertEqual ( <EOL> NonMatch ( "<STR_LIT:a>" , "<STR_LIT:b>" ) , <EOL> NonMatch ( "<STR_LIT:a>" , String ( "<STR_LIT:b>" ) ) ) <EOL> self . assertEqual ( <EOL> NonMatch ( "<STR_LIT:a>" , re . compile ( "<STR_LIT:b>" ) ) , <EOL> NonMatch ( "<STR_LIT:a>" , RegExp ( "<STR_LIT:b>" ) ) ) <EOL> def test_domainname ( self ) : <EOL> matcher = NonMatch ( "<STR_LIT:a>" , DomainName ( "<STR_LIT>" ) ) <EOL> self . assertFalse ( matcher . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> self . assertFalse ( matcher . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> self . assertTrue ( matcher . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> self . assertTrue ( matcher . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> self . assertTrue ( matcher . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> _options = [ <EOL> NonMatch ( ) , <EOL> NonMatch ( "<STR_LIT:a>" , String ( "<STR_LIT:b>" ) ) , <EOL> NonMatch ( "<STR_LIT:a>" , RegExp ( "<STR_LIT:b>" ) ) , <EOL> NonMatch ( "<STR_LIT:a>" , IP ( "<STR_LIT>" ) ) , <EOL> NonMatch ( "<STR_LIT:a>" , DomainName ( "<STR_LIT>" ) ) <EOL> ] <EOL> def test_pickling_and_unpickling ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , pickle . loads ( pickle . dumps ( option ) ) ) <EOL> def test_repr ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , eval ( repr ( option ) ) ) <EOL> class TestFuzzy ( unittest . TestCase ) : <EOL> def test_base ( self ) : <EOL> rule = Fuzzy ( String ( "<STR_LIT:a>" ) ) <EOL> self . assertTrue ( rule . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> self . assertTrue ( rule . match ( Event ( { "<STR_LIT>" : "<STR_LIT:a>" } ) ) ) <EOL> self . assertTrue ( rule . match ( Event ( { "<STR_LIT>" : "<STR_LIT>" } ) ) ) <EOL> self . assertTrue ( rule . match ( Event ( { "<STR_LIT>" : "<STR_LIT>" } ) ) ) <EOL> self . assertFalse ( rule . match ( Event ( { "<STR_LIT>" : "<STR_LIT>" } ) ) ) <EOL> self . assertTrue ( rule . match ( Event ( { "<STR_LIT:A>" : "<STR_LIT>" } ) ) ) <EOL> self . assertTrue ( rule . match ( Event ( { "<STR_LIT>" : "<STR_LIT:A>" } ) ) ) <EOL> rule = Fuzzy ( RegExp ( "<STR_LIT:a>" ) ) <EOL> self . assertTrue ( rule . match ( Event ( { "<STR_LIT:a>" : "<STR_LIT>" } ) ) ) <EOL> self . assertTrue ( rule . match ( Event ( { "<STR_LIT>" : "<STR_LIT:a>" } ) ) ) <EOL> self . assertTrue ( rule . match ( Event ( { "<STR_LIT>" : "<STR_LIT>" } ) ) ) <EOL> self . assertTrue ( rule . match ( Event ( { "<STR_LIT>" : "<STR_LIT>" } ) ) ) <EOL> self . assertFalse ( rule . match ( Event ( { "<STR_LIT>" : "<STR_LIT>" } ) ) ) <EOL> self . assertFalse ( rule . match ( Event ( { "<STR_LIT:A>" : "<STR_LIT>" } ) ) ) <EOL> self . assertFalse ( rule . match ( Event ( { "<STR_LIT>" : "<STR_LIT:A>" } ) ) ) <EOL> _options = [ <EOL> Fuzzy ( String ( "<STR_LIT:a>" ) ) , <EOL> Fuzzy ( RegExp ( "<STR_LIT:a>" ) ) , <EOL> Fuzzy ( IP ( "<STR_LIT>" ) ) , <EOL> Fuzzy ( DomainName ( "<STR_LIT>" ) ) <EOL> ] <EOL> def test_pickling_and_unpickling ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , pickle . loads ( pickle . dumps ( option ) ) ) <EOL> def test_repr ( self ) : <EOL> for option in self . _options : <EOL> self . assertEqual ( option , eval ( repr ( option ) ) ) </s>
