1*d4726bddSHONG Yifan"""Common test helpers for unit tests.""" 2*d4726bddSHONG Yifan 3*d4726bddSHONG Yifanload("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") 4*d4726bddSHONG Yifan 5*d4726bddSHONG Yifandef assert_argv_contains_not(env, action, flag): 6*d4726bddSHONG Yifan asserts.true( 7*d4726bddSHONG Yifan env, 8*d4726bddSHONG Yifan flag not in action.argv, 9*d4726bddSHONG Yifan "Expected {args} to not contain {flag}".format(args = action.argv, flag = flag), 10*d4726bddSHONG Yifan ) 11*d4726bddSHONG Yifan 12*d4726bddSHONG Yifandef assert_argv_contains(env, action, flag): 13*d4726bddSHONG Yifan asserts.true( 14*d4726bddSHONG Yifan env, 15*d4726bddSHONG Yifan flag in action.argv, 16*d4726bddSHONG Yifan "Expected {args} to contain {flag}".format(args = action.argv, flag = flag), 17*d4726bddSHONG Yifan ) 18*d4726bddSHONG Yifan 19*d4726bddSHONG Yifandef assert_argv_contains_prefix_suffix(env, action, prefix, suffix): 20*d4726bddSHONG Yifan for found_flag in action.argv: 21*d4726bddSHONG Yifan if found_flag.startswith(prefix) and found_flag.endswith(suffix): 22*d4726bddSHONG Yifan return 23*d4726bddSHONG Yifan unittest.fail( 24*d4726bddSHONG Yifan env, 25*d4726bddSHONG Yifan "Expected an arg with prefix '{prefix}' and suffix '{suffix}' in {args}".format( 26*d4726bddSHONG Yifan prefix = prefix, 27*d4726bddSHONG Yifan suffix = suffix, 28*d4726bddSHONG Yifan args = action.argv, 29*d4726bddSHONG Yifan ), 30*d4726bddSHONG Yifan ) 31*d4726bddSHONG Yifan 32*d4726bddSHONG Yifandef assert_argv_contains_prefix(env, action, prefix): 33*d4726bddSHONG Yifan for found_flag in action.argv: 34*d4726bddSHONG Yifan if found_flag.startswith(prefix): 35*d4726bddSHONG Yifan return 36*d4726bddSHONG Yifan unittest.fail( 37*d4726bddSHONG Yifan env, 38*d4726bddSHONG Yifan "Expected an arg with prefix '{prefix}' in {args}".format( 39*d4726bddSHONG Yifan prefix = prefix, 40*d4726bddSHONG Yifan args = action.argv, 41*d4726bddSHONG Yifan ), 42*d4726bddSHONG Yifan ) 43*d4726bddSHONG Yifan 44*d4726bddSHONG Yifandef assert_argv_contains_prefix_not(env, action, prefix): 45*d4726bddSHONG Yifan for found_flag in action.argv: 46*d4726bddSHONG Yifan if found_flag.startswith(prefix): 47*d4726bddSHONG Yifan unittest.fail( 48*d4726bddSHONG Yifan env, 49*d4726bddSHONG Yifan "Expected an arg with prefix '{prefix}' to not appear in {args}".format( 50*d4726bddSHONG Yifan prefix = prefix, 51*d4726bddSHONG Yifan args = action.argv, 52*d4726bddSHONG Yifan ), 53*d4726bddSHONG Yifan ) 54*d4726bddSHONG Yifan 55*d4726bddSHONG Yifandef assert_action_mnemonic(env, action, mnemonic): 56*d4726bddSHONG Yifan if not action.mnemonic == mnemonic: 57*d4726bddSHONG Yifan unittest.fail( 58*d4726bddSHONG Yifan env, 59*d4726bddSHONG Yifan "Expected the action to have the mnemonic '{expected}', but got '{actual}'".format( 60*d4726bddSHONG Yifan expected = mnemonic, 61*d4726bddSHONG Yifan actual = action.mnemonic, 62*d4726bddSHONG Yifan ), 63*d4726bddSHONG Yifan ) 64*d4726bddSHONG Yifan 65*d4726bddSHONG Yifandef _startswith(list, prefix): 66*d4726bddSHONG Yifan if len(list) < len(prefix): 67*d4726bddSHONG Yifan return False 68*d4726bddSHONG Yifan for pair in zip(list[:len(prefix) + 1], prefix): 69*d4726bddSHONG Yifan if pair[0] != pair[1]: 70*d4726bddSHONG Yifan return False 71*d4726bddSHONG Yifan return True 72*d4726bddSHONG Yifan 73*d4726bddSHONG Yifandef assert_list_contains_adjacent_elements(env, list_under_test, adjacent_elements): 74*d4726bddSHONG Yifan """Assert that list_under_test contains given adjacent flags. 75*d4726bddSHONG Yifan 76*d4726bddSHONG Yifan Args: 77*d4726bddSHONG Yifan env: env from analysistest.begin(ctx). 78*d4726bddSHONG Yifan list_under_test: list supposed to contain adjacent elements. 79*d4726bddSHONG Yifan adjacent_elements: list of elements to be found inside list_under_test. 80*d4726bddSHONG Yifan """ 81*d4726bddSHONG Yifan for idx in range(len(list_under_test)): 82*d4726bddSHONG Yifan if list_under_test[idx] == adjacent_elements[0]: 83*d4726bddSHONG Yifan if _startswith(list_under_test[idx:], adjacent_elements): 84*d4726bddSHONG Yifan return 85*d4726bddSHONG Yifan 86*d4726bddSHONG Yifan unittest.fail( 87*d4726bddSHONG Yifan env, 88*d4726bddSHONG Yifan "Expected the to find '{expected}' within '{actual}'".format( 89*d4726bddSHONG Yifan expected = adjacent_elements, 90*d4726bddSHONG Yifan actual = list_under_test, 91*d4726bddSHONG Yifan ), 92*d4726bddSHONG Yifan ) 93*d4726bddSHONG Yifan 94*d4726bddSHONG Yifandef assert_list_contains_adjacent_elements_not(env, list_under_test, adjacent_elements): 95*d4726bddSHONG Yifan """Assert that list_under_test does not contains given adjacent flags. 96*d4726bddSHONG Yifan 97*d4726bddSHONG Yifan Args: 98*d4726bddSHONG Yifan env: env from analysistest.begin(ctx). 99*d4726bddSHONG Yifan list_under_test: list supposed not to contain adjacent elements. 100*d4726bddSHONG Yifan adjacent_elements: list of elements not to be found inside list_under_test.""" 101*d4726bddSHONG Yifan for idx in range(len(list_under_test)): 102*d4726bddSHONG Yifan if list_under_test[idx] == adjacent_elements[0]: 103*d4726bddSHONG Yifan if _startswith(list_under_test[idx:], adjacent_elements): 104*d4726bddSHONG Yifan unittest.fail( 105*d4726bddSHONG Yifan env, 106*d4726bddSHONG Yifan "Expected not the to find '{expected}' within '{actual}'".format( 107*d4726bddSHONG Yifan expected = adjacent_elements, 108*d4726bddSHONG Yifan actual = list_under_test, 109*d4726bddSHONG Yifan ), 110*d4726bddSHONG Yifan ) 111*d4726bddSHONG Yifan 112*d4726bddSHONG Yifandef assert_env_value(env, action, key, value): 113*d4726bddSHONG Yifan asserts.true( 114*d4726bddSHONG Yifan env, 115*d4726bddSHONG Yifan action.env[key] == value, 116*d4726bddSHONG Yifan "Expected env[{key}] to be equal to '{value}', got '{real_value}'".format( 117*d4726bddSHONG Yifan key = key, 118*d4726bddSHONG Yifan value = value, 119*d4726bddSHONG Yifan real_value = action.env[key], 120*d4726bddSHONG Yifan ), 121*d4726bddSHONG Yifan ) 122