mirror of
https://github.com/facebook/sapling.git
synced 2024-10-08 15:57:43 +03:00
add makeTestHash() helper function
Summary: Add a helper function to create human-readable hashes in test code without having to always specify a full 40-byte long string. Reviewed By: wez Differential Revision: D4336161 fbshipit-source-id: cf6af58dd788b5553a2a6daef56db43cddbbc04a
This commit is contained in:
parent
0e4b65bb85
commit
26833281bf
32
eden/fs/testharness/TestUtil.cpp
Normal file
32
eden/fs/testharness/TestUtil.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2016, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
*/
|
||||
#include "TestUtil.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include "eden/fs/model/Hash.h"
|
||||
|
||||
namespace facebook {
|
||||
namespace eden {
|
||||
Hash makeTestHash(folly::StringPiece value) {
|
||||
constexpr size_t ASCII_SIZE = 2 * Hash::RAW_SIZE;
|
||||
if (value.size() > ASCII_SIZE) {
|
||||
throw std::invalid_argument(value.toString() + " is too big for Hash");
|
||||
}
|
||||
std::array<char, ASCII_SIZE> fullValue;
|
||||
memset(fullValue.data(), '0', fullValue.size());
|
||||
memcpy(
|
||||
fullValue.data() + fullValue.size() - value.size(),
|
||||
value.data(),
|
||||
value.size());
|
||||
return Hash{folly::StringPiece{folly::range(fullValue)}};
|
||||
}
|
||||
}
|
||||
}
|
27
eden/fs/testharness/TestUtil.h
Normal file
27
eden/fs/testharness/TestUtil.h
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (c) 2016, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <folly/Range.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace eden {
|
||||
|
||||
class Hash;
|
||||
|
||||
/**
|
||||
* Helper function for creating Hash values to use in tests.
|
||||
*
|
||||
* The input should be an ASCII hex string. It may be less than 40-bytes long,
|
||||
* in which case it will be sign-extended to 40 bytes.
|
||||
*/
|
||||
Hash makeTestHash(folly::StringPiece value);
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ cpp_unittest(
|
||||
srcs = glob(['*Test.cpp']),
|
||||
deps = [
|
||||
'//eden/fs/testharness:testharness',
|
||||
'//eden/utils/test:test_lib',
|
||||
],
|
||||
external_deps = [
|
||||
'gtest',
|
||||
|
40
eden/fs/testharness/test/TestUtilTest.cpp
Normal file
40
eden/fs/testharness/test/TestUtilTest.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2016, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
*/
|
||||
#include "eden/fs/testharness/TestUtil.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "eden/fs/model/Hash.h"
|
||||
#include "eden/utils/test/TestChecks.h"
|
||||
|
||||
using namespace facebook::eden;
|
||||
|
||||
TEST(TestUtil, makeTestHash) {
|
||||
EXPECT_EQ(
|
||||
Hash{"0000000000000000000000000000000000000001"}, makeTestHash("1"));
|
||||
EXPECT_EQ(
|
||||
Hash{"0000000000000000000000000000000000000022"}, makeTestHash("22"));
|
||||
EXPECT_EQ(
|
||||
Hash{"0000000000000000000000000000000000000abc"}, makeTestHash("abc"));
|
||||
EXPECT_EQ(
|
||||
Hash{"123456789abcdef0fedcba9876543210faceb00c"},
|
||||
makeTestHash("123456789abcdef0fedcba9876543210faceb00c"));
|
||||
EXPECT_EQ(Hash{"0000000000000000000000000000000000000000"}, makeTestHash(""));
|
||||
EXPECT_THROW_RE(
|
||||
makeTestHash("123456789abcdef0fedcba9876543210faceb00c1"),
|
||||
std::invalid_argument,
|
||||
"too big");
|
||||
EXPECT_THROW_RE(
|
||||
makeTestHash("z"), std::exception, "could not be unhexlified");
|
||||
EXPECT_THROW_RE(
|
||||
// There's a "g" in the string below
|
||||
makeTestHash("123456789abcdefgfedcba9876543210faceb00c"),
|
||||
std::exception,
|
||||
"could not be unhexlified");
|
||||
}
|
Loading…
Reference in New Issue
Block a user