Welcome to mirror list, hosted at ThFree Co, Russian Federation.

uri_test.cpp « coding_tests « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c6676ea112be55077020acd3e53b4a38c7a74fb2 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "testing/testing.hpp"
#include "coding/uri.hpp"
#include "base/macros.hpp"
#include "std/bind.hpp"
#include "std/queue.hpp"
#include "std/utility.hpp"

using url_scheme::Uri;

namespace
{

class TestUri
{
public:
  TestUri(string const & uri) { m_uri = uri; }
  TestUri & Scheme(string const &scheme) { m_scheme = scheme; return *this; }
  TestUri & Path(string const & path) { m_path = path; return *this; }
  TestUri & KV(string const & key, string const & value)
  {
    m_keyValuePairs.push(make_pair(key, value));
    return *this;
  }

  ~TestUri()
  {
    Uri uri(m_uri);
    TEST_EQUAL(uri.GetScheme(), m_scheme, ());
    TEST_EQUAL(uri.GetPath(), m_path, ());
    TEST(!m_scheme.empty() || !uri.IsValid(), ("Scheme is empty if and only if uri is invalid!"));
    uri.ForEachKeyValue(bind(&TestUri::AddTestValue, this, _1, _2));
  }

private:

  bool AddTestValue(string const & key, string const & value)
  {
    TEST(!m_keyValuePairs.empty(), ("Failed for uri = ", m_uri, "Passed KV = ", key, value));
    TEST_EQUAL(m_keyValuePairs.front().first,  key, ());
    TEST_EQUAL(m_keyValuePairs.front().second, value, ());
    m_keyValuePairs.pop();
    return true;
  }

  string m_uri, m_scheme, m_path;
  queue<pair<string, string> > m_keyValuePairs;
};

}  // unnamed namespace

UNIT_TEST(UriValidScheme)
{
  char const uriS[] = "mapswithme://map?ll=10.3,12.3223&n=Hello%20World";
  Uri uri(uriS, ARRAY_SIZE(uriS) - 1);
  TEST_EQUAL(uri.GetScheme(), "mapswithme", ());
}

UNIT_TEST(UriInvalidSchemeNoColon)
{
  TEST_EQUAL(Uri("mapswithme:").GetScheme(), "mapswithme", ());
}

UNIT_TEST(UriTestValidScheme2)
{
  TestUri("mapswithme://map?ll=10.3,12.3223&n=Hello%20World")
      .Scheme("mapswithme")
      .Path("map")
      .KV("ll", "10.3,12.3223")
      .KV("n", "Hello World");
}

UNIT_TEST(UriComprehensive)
{
  TestUri("");

  TestUri("scheme:").Scheme("scheme");

  TestUri("scheme:/").Scheme("scheme");

  TestUri("scheme://").Scheme("scheme");

  TestUri("sometext");

  TestUri(":noscheme");

  TestUri("://noscheme?");

  TestUri("mwm://?").Scheme("mwm");

  TestUri("http://path/to/something").Scheme("http").Path("path/to/something");

  TestUri("http://path?").Scheme("http").Path("path");

  TestUri("maps://path?&&key=&").Scheme("maps").Path("path").KV("key", "");

  TestUri("mapswithme://map?ll=1.2,3.4&z=15").Scheme("mapswithme").Path("map")
      .KV("ll", "1.2,3.4").KV("z", "15");

  TestUri("nopathnovalues://?key1&key2=val2").Scheme("nopathnovalues").Path("")
      .KV("key1", "").KV("key2", "val2");

  TestUri("s://?key1&key2").Scheme("s").Path("").KV("key1", "").KV("key2", "");

  TestUri("g://p?key1=val1&key2=").Scheme("g").Path("p").KV("key1", "val1").KV("key2", "");

  TestUri("g://p?=val1&key2=").Scheme("g").Path("p").KV("", "val1").KV("key2", "");

  TestUri("g://?k&key2").Scheme("g").KV("k", "").KV("key2", "");

  TestUri("m:?%26Amp%26%3D%26Amp%26&name=%31%20%30").Scheme("m")
      .KV("&Amp&=&Amp&", "").KV("name", "1 0");

  TestUri("s://?key1=value1&key1=value2&key1=value3&key2&key2&key3=value1&key3&key3=value2")
      .Scheme("s")
      .KV("key1", "value1").KV("key1", "value2").KV("key1", "value3")
      .KV("key2", "").KV("key2", "")
      .KV("key3", "value1").KV("key3", "").KV("key3", "value2");
}