def testTraceDidntImport(self):
        run_info = run_info_module.RunInfo('file:///a.json',
                                           '/a.json',
                                           metadata={'m': 1})
        trace_string = 'This is intentionally not a trace-formatted string.'
        trace_handle = in_memory_trace_handle.InMemoryTraceHandle(
            run_info, trace_string)

        results = results_module.Results()
        with map_single_trace.TemporaryMapScript("""
      pi.MapFunction.register(function MyMapFunction(results, run_info, model) {
      });
    """) as map_script:
            map_single_trace.MapSingleTrace(results, trace_handle,
                                            map_script.filename)

        self.assertEquals(len(results.all_values), 1)
        v = results.all_values[0]
        self.assertIsInstance(v, map_single_trace.TraceImportErrorValue)
    def testMapperSkips(self):
        run_info = run_info_module.RunInfo('file:///a.json',
                                           '/a.json',
                                           metadata={'m': 1})
        events = [{
            'pid': 1,
            'tid': 2,
            'ph': 'X',
            'name': 'a',
            'cat': 'c',
            'ts': 0,
            'dur': 10,
            'args': {}
        }, {
            'pid': 1,
            'tid': 2,
            'ph': 'X',
            'name': 'b',
            'cat': 'c',
            'ts': 3,
            'dur': 5,
            'args': {}
        }]
        trace_handle = in_memory_trace_handle.InMemoryTraceHandle(
            run_info, json.dumps(events))

        results = results_module.Results()
        with map_single_trace.TemporaryMapScript("""
      pi.MapFunction.register(function MyMapFunction(results, run_info, model) {
        results.addValue(new pi.v.SkipValue(
            run_info, 'SkippedFieldName',
            {description: 'SkippedReason'}));

      });
    """) as map_script:
            map_single_trace.MapSingleTrace(results, trace_handle,
                                            map_script.filename)

        self.assertEquals(len(results.all_values), 1)
        v = results.all_values[0]
        self.assertIsInstance(v, value_module.SkipValue)
        self.assertEquals(v.name, 'SkippedFieldName')
        self.assertEquals(v.description, 'SkippedReason')
    def testPassingMapScript(self):
        run_info = run_info_module.RunInfo('file:///a.json',
                                           '/a.json',
                                           metadata={'m': 1})
        events = [{
            'pid': 1,
            'tid': 2,
            'ph': 'X',
            'name': 'a',
            'cat': 'c',
            'ts': 0,
            'dur': 10,
            'args': {}
        }, {
            'pid': 1,
            'tid': 2,
            'ph': 'X',
            'name': 'b',
            'cat': 'c',
            'ts': 3,
            'dur': 5,
            'args': {}
        }]
        trace_handle = in_memory_trace_handle.InMemoryTraceHandle(
            run_info, json.dumps(events))

        results = results_module.Results()
        with map_single_trace.TemporaryMapScript("""
      pi.MapFunction.register(function MyMapFunction(results, run_info, model) {
        results.addValue(new pi.v.DictValue(
            run_info,
            'result', {
              numProcesses: model.getAllProcesses().length
            }));
      });
    """) as map_script:
            map_single_trace.MapSingleTrace(results, trace_handle,
                                            map_script.filename)

        v = results.FindValueNamed('result')
        self.assertEquals(v['numProcesses'], 1)
    def testMapFunctionThatThrows(self):
        run_info = run_info_module.RunInfo('file:///a.json',
                                           '/a.json',
                                           metadata={'m': 1})
        events = [{
            'pid': 1,
            'tid': 2,
            'ph': 'X',
            'name': 'a',
            'cat': 'c',
            'ts': 0,
            'dur': 10,
            'args': {}
        }, {
            'pid': 1,
            'tid': 2,
            'ph': 'X',
            'name': 'b',
            'cat': 'c',
            'ts': 3,
            'dur': 5,
            'args': {}
        }]
        trace_handle = in_memory_trace_handle.InMemoryTraceHandle(
            run_info, json.dumps(events))

        results = results_module.Results()
        with map_single_trace.TemporaryMapScript("""
      pi.MapFunction.register(function MyMapFunction(results, run_info, model) {
        throw new Error('Expected error');
      });
    """) as map_script:
            map_single_trace.MapSingleTrace(results, trace_handle,
                                            map_script.filename)

        self.assertEquals(len(results.all_values), 1)
        v = results.all_values[0]
        self.assertIsInstance(v, map_single_trace.MapFunctionErrorValue)
    def testNoMapper(self):
        run_info = run_info_module.RunInfo('file:///a.json',
                                           '/a.json',
                                           metadata={'m': 1})
        events = [{
            'pid': 1,
            'tid': 2,
            'ph': 'X',
            'name': 'a',
            'cat': 'c',
            'ts': 0,
            'dur': 10,
            'args': {}
        }, {
            'pid': 1,
            'tid': 2,
            'ph': 'X',
            'name': 'b',
            'cat': 'c',
            'ts': 3,
            'dur': 5,
            'args': {}
        }]
        trace_handle = in_memory_trace_handle.InMemoryTraceHandle(
            run_info, json.dumps(events))

        results = results_module.Results()
        with map_single_trace.TemporaryMapScript("""
    """) as map_script:
            map_single_trace.MapSingleTrace(results, trace_handle,
                                            map_script.filename)

        self.assertEquals(len(results.all_values), 1)
        v = results.all_values[0]
        self.assertIsInstance(v,
                              map_single_trace.MapFunctionNotDefinedErrorValue)