summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/unit/test_longstringactor.js
blob: 18b92891007b15d341c4d1e50c695aa54f965330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* -*- indent-tabs-mode: nil; js-indent-level: 2; js-indent-level: 2 -*- */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { LongStringActor } = require("devtools/server/actors/object");

function run_test() {
  test_LSA_disconnect();
  test_LSA_grip();
  test_LSA_onSubstring();
}

const TEST_STRING = "This is a very long string!";

function makeMockLongStringActor()
{
  let string = TEST_STRING;
  let actor = new LongStringActor(string);
  actor.actorID = "longString1";
  actor.registeredPool = {
    longStringActors: {
      [string]: actor
    }
  };
  return actor;
}

function test_LSA_disconnect()
{
  let actor = makeMockLongStringActor();
  do_check_eq(actor.registeredPool.longStringActors[TEST_STRING], actor);

  actor.disconnect();
  do_check_eq(actor.registeredPool.longStringActors[TEST_STRING], void 0);
}

function test_LSA_substring()
{
  let actor = makeMockLongStringActor();
  do_check_eq(actor._substring(0, 4), TEST_STRING.substring(0, 4));
  do_check_eq(actor._substring(6, 9), TEST_STRING.substring(6, 9));
  do_check_eq(actor._substring(0, TEST_STRING.length), TEST_STRING);
}

function test_LSA_grip()
{
  let actor = makeMockLongStringActor();

  let grip = actor.grip();
  do_check_eq(grip.type, "longString");
  do_check_eq(grip.initial, TEST_STRING.substring(0, DebuggerServer.LONG_STRING_INITIAL_LENGTH));
  do_check_eq(grip.length, TEST_STRING.length);
  do_check_eq(grip.actor, actor.actorID);
}

function test_LSA_onSubstring()
{
  let actor = makeMockLongStringActor();
  let response;

  // From the start
  response = actor.onSubstring({
    start: 0,
    end: 4
  });
  do_check_eq(response.from, actor.actorID);
  do_check_eq(response.substring, TEST_STRING.substring(0, 4));

  // In the middle
  response = actor.onSubstring({
    start: 5,
    end: 8
  });
  do_check_eq(response.from, actor.actorID);
  do_check_eq(response.substring, TEST_STRING.substring(5, 8));

  // Whole string
  response = actor.onSubstring({
    start: 0,
    end: TEST_STRING.length
  });
  do_check_eq(response.from, actor.actorID);
  do_check_eq(response.substring, TEST_STRING);

  // Negative index
  response = actor.onSubstring({
    start: -5,
    end: TEST_STRING.length
  });
  do_check_eq(response.from, actor.actorID);
  do_check_eq(response.substring,
              TEST_STRING.substring(-5, TEST_STRING.length));

  // Past the end
  response = actor.onSubstring({
    start: TEST_STRING.length - 5,
    end: 100
  });
  do_check_eq(response.from, actor.actorID);
  do_check_eq(response.substring,
              TEST_STRING.substring(TEST_STRING.length - 5, 100));
}