rust/hg-core/src/dirstate/dirs_multiset.rs
author Raphaël Gomès <rgomes@octobus.net>
Wed, 17 Jul 2019 11:37:43 +0200
changeset 42766 849e744b925d
parent 42765 7ceded4419a3
child 42818 2e1f74cc3350
permissions -rw-r--r--
rust-dirstate: improve API of `DirsMultiset` - Use opaque `Iterator` type instead of implementation-specific one from `HashMap` - Make `DirsMultiset` behave like a set both in Rust and from Python Differential Revision: https://phab.mercurial-scm.org/D6690
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
// dirs_multiset.rs
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
//
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
//
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
//! A multiset of directory names.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     9
//!
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    10
//! Used to counts the references to directories in a manifest or dirstate.
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    11
use crate::{
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    12
    dirstate::EntryState, utils::files, DirsIterable, DirstateEntry,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    13
    DirstateMapError,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    14
};
42766
849e744b925d rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 42765
diff changeset
    15
use std::collections::hash_map::Entry;
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    16
use std::collections::HashMap;
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    17
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    18
#[derive(PartialEq, Debug)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    19
pub struct DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    20
    inner: HashMap<Vec<u8>, u32>,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
}
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    22
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    23
impl DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    24
    /// Initializes the multiset from a dirstate or a manifest.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
    ///
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
    /// If `skip_state` is provided, skips dirstate entries with equal state.
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    27
    pub fn new(
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    28
        iterable: DirsIterable,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    29
        skip_state: Option<EntryState>,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
    30
    ) -> Self {
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    31
        let mut multiset = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    32
            inner: HashMap::new(),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    33
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    34
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    35
        match iterable {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    36
            DirsIterable::Dirstate(vec) => {
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    37
                for (filename, DirstateEntry { state, .. }) in vec {
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    38
                    // This `if` is optimized out of the loop
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    39
                    if let Some(skip) = skip_state {
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    40
                        if skip != *state {
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
                            multiset.add_path(filename);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    42
                        }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
                    } else {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
                        multiset.add_path(filename);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    45
                    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
                }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    47
            }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    48
            DirsIterable::Manifest(vec) => {
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
    49
                for filename in vec {
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
                    multiset.add_path(filename);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
                }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
            }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
        }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
        multiset
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    56
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
    /// Increases the count of deepest directory contained in the path.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    59
    ///
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
    /// If the directory is not yet in the map, adds its parents.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
    pub fn add_path(&mut self, path: &[u8]) {
42608
717686c5e461 rust-utils: use new find_dirs iterator
Raphaël Gomès <rgomes@octobus.net>
parents: 42562
diff changeset
    62
        for subpath in files::find_dirs(path) {
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    63
            if let Some(val) = self.inner.get_mut(subpath) {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    64
                *val += 1;
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    65
                break;
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    66
            }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    67
            self.inner.insert(subpath.to_owned(), 1);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    68
        }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    69
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    70
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    71
    /// Decreases the count of deepest directory contained in the path.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    72
    ///
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    73
    /// If it is the only reference, decreases all parents until one is
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
    /// removed.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    75
    /// If the directory is not in the map, something horrible has happened.
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    76
    pub fn delete_path(
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    77
        &mut self,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    78
        path: &[u8],
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    79
    ) -> Result<(), DirstateMapError> {
42608
717686c5e461 rust-utils: use new find_dirs iterator
Raphaël Gomès <rgomes@octobus.net>
parents: 42562
diff changeset
    80
        for subpath in files::find_dirs(path) {
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    81
            match self.inner.entry(subpath.to_owned()) {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    82
                Entry::Occupied(mut entry) => {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    83
                    let val = entry.get().clone();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    84
                    if val > 1 {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    85
                        entry.insert(val - 1);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    86
                        break;
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    87
                    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    88
                    entry.remove();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    89
                }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    90
                Entry::Vacant(_) => {
42560
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42543
diff changeset
    91
                    return Err(DirstateMapError::PathNotFound(
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42543
diff changeset
    92
                        path.to_owned(),
d26e4a434fe5 rust: run rfmt on all hg-core/hg-cpython code
Raphaël Gomès <rgomes@octobus.net>
parents: 42543
diff changeset
    93
                    ))
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    94
                }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    95
            };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    96
        }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    97
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    98
        Ok(())
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    99
    }
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   100
42766
849e744b925d rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 42765
diff changeset
   101
    pub fn contains(&self, key: &[u8]) -> bool {
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   102
        self.inner.contains_key(key)
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   103
    }
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   104
42766
849e744b925d rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 42765
diff changeset
   105
    pub fn iter(&self) -> impl Iterator<Item = &Vec<u8>> {
849e744b925d rust-dirstate: improve API of `DirsMultiset`
Raphaël Gomès <rgomes@octobus.net>
parents: 42765
diff changeset
   106
        self.inner.keys()
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   107
    }
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   108
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   109
    pub fn len(&self) -> usize {
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   110
        self.inner.len()
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   111
    }
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   112
}
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   113
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   114
#[cfg(test)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   115
mod tests {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   116
    use super::*;
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   117
    use std::collections::HashMap;
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   118
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   119
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   120
    fn test_delete_path_path_not_found() {
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   121
        let mut map = DirsMultiset::new(DirsIterable::Manifest(&vec![]), None);
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   122
        let path = b"doesnotexist/";
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   123
        assert_eq!(
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   124
            Err(DirstateMapError::PathNotFound(path.to_vec())),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   125
            map.delete_path(path)
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   126
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   127
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   128
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   129
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   130
    fn test_delete_path_empty_path() {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   131
        let mut map =
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   132
            DirsMultiset::new(DirsIterable::Manifest(&vec![vec![]]), None);
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   133
        let path = b"";
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   134
        assert_eq!(Ok(()), map.delete_path(path));
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   135
        assert_eq!(
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   136
            Err(DirstateMapError::PathNotFound(path.to_vec())),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   137
            map.delete_path(path)
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   138
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   139
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   140
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   141
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   142
    fn test_delete_path_successful() {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   143
        let mut map = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   144
            inner: [("", 5), ("a", 3), ("a/b", 2), ("a/c", 1)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   145
                .iter()
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   146
                .map(|(k, v)| (k.as_bytes().to_vec(), *v))
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   147
                .collect(),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   148
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   149
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   150
        assert_eq!(Ok(()), map.delete_path(b"a/b/"));
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   151
        assert_eq!(Ok(()), map.delete_path(b"a/b/"));
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   152
        assert_eq!(
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   153
            Err(DirstateMapError::PathNotFound(b"a/b/".to_vec())),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   154
            map.delete_path(b"a/b/")
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   155
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   156
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   157
        assert_eq!(2, *map.inner.get(&b"a".to_vec()).unwrap());
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   158
        assert_eq!(1, *map.inner.get(&b"a/c".to_vec()).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   159
        eprintln!("{:?}", map);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   160
        assert_eq!(Ok(()), map.delete_path(b"a/"));
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   161
        eprintln!("{:?}", map);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   162
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   163
        assert_eq!(Ok(()), map.delete_path(b"a/c/"));
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   164
        assert_eq!(
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   165
            Err(DirstateMapError::PathNotFound(b"a/c/".to_vec())),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   166
            map.delete_path(b"a/c/")
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   167
        );
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   168
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   169
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   170
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   171
    fn test_add_path_empty_path() {
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   172
        let mut map = DirsMultiset::new(DirsIterable::Manifest(&vec![]), None);
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   173
        let path = b"";
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   174
        map.add_path(path);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   175
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   176
        assert_eq!(1, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   177
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   178
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   179
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   180
    fn test_add_path_successful() {
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   181
        let mut map = DirsMultiset::new(DirsIterable::Manifest(&vec![]), None);
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   182
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   183
        map.add_path(b"a/");
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   184
        assert_eq!(1, *map.inner.get(&b"a".to_vec()).unwrap());
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   185
        assert_eq!(1, *map.inner.get(&Vec::new()).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   186
        assert_eq!(2, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   187
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   188
        // Non directory should be ignored
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   189
        map.add_path(b"a");
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   190
        assert_eq!(1, *map.inner.get(&b"a".to_vec()).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   191
        assert_eq!(2, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   192
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   193
        // Non directory will still add its base
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   194
        map.add_path(b"a/b");
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   195
        assert_eq!(2, *map.inner.get(&b"a".to_vec()).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   196
        assert_eq!(2, map.len());
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   197
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   198
        // Duplicate path works
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   199
        map.add_path(b"a/");
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   200
        assert_eq!(3, *map.inner.get(&b"a".to_vec()).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   201
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   202
        // Nested dir adds to its base
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   203
        map.add_path(b"a/b/");
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   204
        assert_eq!(4, *map.inner.get(&b"a".to_vec()).unwrap());
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   205
        assert_eq!(1, *map.inner.get(&b"a/b".to_vec()).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   206
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   207
        // but not its base's base, because it already existed
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   208
        map.add_path(b"a/b/c/");
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   209
        assert_eq!(4, *map.inner.get(&b"a".to_vec()).unwrap());
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   210
        assert_eq!(2, *map.inner.get(&b"a/b".to_vec()).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   211
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   212
        map.add_path(b"a/c/");
42562
a80464e85ddd rust: remove Deref in favor of explicit methods
Raphaël Gomès <rgomes@octobus.net>
parents: 42560
diff changeset
   213
        assert_eq!(1, *map.inner.get(&b"a/c".to_vec()).unwrap());
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   214
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   215
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   216
            inner: [("", 2), ("a", 5), ("a/b", 2), ("a/b/c", 1), ("a/c", 1)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   217
                .iter()
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   218
                .map(|(k, v)| (k.as_bytes().to_vec(), *v))
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   219
                .collect(),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   220
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   221
        assert_eq!(map, expected);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   222
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   223
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   224
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   225
    fn test_dirsmultiset_new_empty() {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   226
        use DirsIterable::{Dirstate, Manifest};
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   227
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   228
        let new = DirsMultiset::new(Manifest(&vec![]), None);
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   229
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   230
            inner: HashMap::new(),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   231
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   232
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   233
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   234
        let new = DirsMultiset::new(Dirstate(&HashMap::new()), None);
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   235
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   236
            inner: HashMap::new(),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   237
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   238
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   239
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   240
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   241
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   242
    fn test_dirsmultiset_new_no_skip() {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   243
        use DirsIterable::{Dirstate, Manifest};
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   244
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   245
        let input_vec = ["a/", "b/", "a/c", "a/d/"]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   246
            .iter()
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   247
            .map(|e| e.as_bytes().to_vec())
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   248
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   249
        let expected_inner = [("", 2), ("a", 3), ("b", 1), ("a/d", 1)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   250
            .iter()
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   251
            .map(|(k, v)| (k.as_bytes().to_vec(), *v))
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   252
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   253
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   254
        let new = DirsMultiset::new(Manifest(&input_vec), None);
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   255
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   256
            inner: expected_inner,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   257
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   258
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   259
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   260
        let input_map = ["a/", "b/", "a/c", "a/d/"]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   261
            .iter()
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   262
            .map(|f| {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   263
                (
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   264
                    f.as_bytes().to_vec(),
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   265
                    DirstateEntry {
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   266
                        state: EntryState::Normal,
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   267
                        mode: 0,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   268
                        mtime: 0,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   269
                        size: 0,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   270
                    },
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   271
                )
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   272
            })
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   273
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   274
        let expected_inner = [("", 2), ("a", 3), ("b", 1), ("a/d", 1)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   275
            .iter()
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   276
            .map(|(k, v)| (k.as_bytes().to_vec(), *v))
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   277
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   278
42764
7cae6bc29ff9 rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents: 42609
diff changeset
   279
        let new = DirsMultiset::new(Dirstate(&input_map), None);
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   280
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   281
            inner: expected_inner,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   282
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   283
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   284
    }
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   285
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   286
    #[test]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   287
    fn test_dirsmultiset_new_skip() {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   288
        use DirsIterable::{Dirstate, Manifest};
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   289
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   290
        let input_vec = ["a/", "b/", "a/c", "a/d/"]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   291
            .iter()
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   292
            .map(|e| e.as_bytes().to_vec())
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   293
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   294
        let expected_inner = [("", 2), ("a", 3), ("b", 1), ("a/d", 1)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   295
            .iter()
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   296
            .map(|(k, v)| (k.as_bytes().to_vec(), *v))
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   297
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   298
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   299
        let new =
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   300
            DirsMultiset::new(Manifest(&input_vec), Some(EntryState::Normal));
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   301
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   302
            inner: expected_inner,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   303
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   304
        // Skip does not affect a manifest
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   305
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   306
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   307
        let input_map = [
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   308
            ("a/", EntryState::Normal),
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   309
            ("a/b/", EntryState::Normal),
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   310
            ("a/c", EntryState::Removed),
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   311
            ("a/d/", EntryState::Merged),
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   312
        ]
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   313
        .iter()
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   314
        .map(|(f, state)| {
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   315
            (
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   316
                f.as_bytes().to_vec(),
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   317
                DirstateEntry {
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   318
                    state: *state,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   319
                    mode: 0,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   320
                    mtime: 0,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   321
                    size: 0,
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   322
                },
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   323
            )
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   324
        })
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   325
        .collect();
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   326
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   327
        // "a" incremented with "a/c" and "a/d/"
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   328
        let expected_inner = [("", 1), ("a", 2), ("a/d", 1)]
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   329
            .iter()
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   330
            .map(|(k, v)| (k.as_bytes().to_vec(), *v))
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   331
            .collect();
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   332
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   333
        let new =
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   334
            DirsMultiset::new(Dirstate(&input_map), Some(EntryState::Normal));
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   335
        let expected = DirsMultiset {
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   336
            inner: expected_inner,
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   337
        };
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   338
        assert_eq!(expected, new);
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   339
    }
42765
7ceded4419a3 rust-dirstate: use EntryState enum instead of literals
Raphaël Gomès <rgomes@octobus.net>
parents: 42764
diff changeset
   340
42543
2dcee6497b0b rust-dirstate: add "dirs" Rust implementation
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   341
}