Signal-iOS/SignalServiceKit/tests/Util/OWSFormatTest.swift
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

53 lines
2.6 KiB
Swift

//
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import XCTest
@testable import SignalServiceKit
class OWSFormatTest: SSKBaseTestSwift {
func testTimeIntervals() throws {
XCTAssertEqual(OWSFormat.localizedDurationString(from: 0), "0:00")
XCTAssertEqual(OWSFormat.localizedDurationString(from: 0.4), "0:00")
XCTAssertEqual(OWSFormat.localizedDurationString(from: 0.6), "0:00")
XCTAssertEqual(OWSFormat.localizedDurationString(from: 0.999), "0:00")
XCTAssertEqual(OWSFormat.localizedDurationString(from: 1), "0:01")
XCTAssertEqual(OWSFormat.localizedDurationString(from: 60), "1:00")
XCTAssertEqual(OWSFormat.localizedDurationString(from: 60+12), "1:12")
XCTAssertEqual(OWSFormat.localizedDurationString(from: 25*60+45), "25:45")
XCTAssertEqual(OWSFormat.localizedDurationString(from: 60*60-1), "59:59")
XCTAssertEqual(OWSFormat.localizedDurationString(from: 60*60), "1:00:00")
XCTAssertEqual(OWSFormat.localizedDurationString(from: 3*60*60+4*60+37), "3:04:37")
}
func testDecimals() throws {
XCTAssertEqual(OWSFormat.localizedDecimalString(from: 0), "0")
XCTAssertEqual(OWSFormat.localizedDecimalString(from: 1), "1")
XCTAssertEqual(OWSFormat.localizedDecimalString(from: 1000), "1,000")
XCTAssertEqual(OWSFormat.localizedDecimalString(from: 1234567), "1,234,567")
}
func testFileSizes() throws {
let kb: Int64 = 1000
let mb: Int64 = 1000 * kb
let gb: Int64 = 1000 * mb
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: 0), "Zero KB")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: 1), "1 byte")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: 60), "60 bytes")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: 1*kb), "1 KB")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: Int64(3.3*Double(kb))), "3 KB")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: Int64(13.5*Double(kb))), "14 KB")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: 100*kb), "100 KB")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: 1*mb), "1 MB")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: Int64(4.32*Double(mb))), "4.3 MB")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: 111*mb), "111 MB")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: 1*gb), "1 GB")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: Int64(2.34*Double(gb))), "2.34 GB")
XCTAssertEqual(OWSFormat.localizedFileSizeString(from: 56*gb), "56 GB")
}
}